单击带有哈希标签的链接时强制刷新

发布于 2024-10-02 07:44:37 字数 792 浏览 0 评论 0原文

我有一个带有 iframe 的页面,然后是侧边栏链接。侧边栏链接指向 iframe 中加载的文章。每个侧边栏链接都有一个类似“/news#1940”的链接,其中#标签后面的数字对应于文章的ID。然后,我使用以下 jQuery 加载我们网站上的新闻页面,然后加载 iframe 中的相应文章。

  var $news_story       = $(window.location.hash.substring(1)),
      $news_number      = $news_story.selector,
      $news_url         = 'http://www.articleurlhere.com/ID=' + $news_number,
      $news_url_default = 'http://www.main_news_feed_url_here.com';
      var $url           = location.pathname;

        if ($news_number.length>0) {
          $('#news-feed').attr('src', $news_url);
        } else {
          $('#news-feed').attr('src', $news_url_default);
        }

当您从外部页面单击侧边栏链接时,这非常有用。但是,当您在新闻页面上单击侧边栏链接时,它不起作用(它将 URL 更改为与链接关联的 #number,但不会重新加载页面或在 iframe 中加载文章)。关于如何使这些链接在新闻页面上正常工作有什么想法吗?

I have a page with an iframe and then sidebar links. The sidebar links lead to articles loaded within the iframe. Each sidebar link has a link like this "/news#1940," where the number following the # tag corresponds to the ID of the article. I then am using the following jQuery to both load the news page on our site and then the appropriate article within the iframe.

  var $news_story       = $(window.location.hash.substring(1)),
      $news_number      = $news_story.selector,
      $news_url         = 'http://www.articleurlhere.com/ID=' + $news_number,
      $news_url_default = 'http://www.main_news_feed_url_here.com';
      var $url           = location.pathname;

        if ($news_number.length>0) {
          $('#news-feed').attr('src', $news_url);
        } else {
          $('#news-feed').attr('src', $news_url_default);
        }

This works great when you click on the sidebar links from an external page. It does not work however when you click on a sidebar link while on the news page (it changes the URL to the #number associated with the link, but does not reload the page or load the article in the iframe). Any ideas on how to make these links work while on the news page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兰花执着 2024-10-09 07:44:37

我认为你需要像这样调用重新加载:

window.frames["news-feed"].location.reload();

I think you need to call reload like this:

window.frames["news-feed"].location.reload();
樱娆 2024-10-09 07:44:37

您是否尝试过执行 $(document).trigger('load');

have you tried doing $(document).trigger('load');

挽清梦 2024-10-09 07:44:37

我在链接中使用哈希值时遇到了同样的问题...当哈希标签更改时,页面不会重新加载。我的解决方案是将哈希标签换成问号。这导致每次链接标签更改时以“?YourLinkLabel”结尾的链接实际上都会重新加载页面(通过单击 href 属性以“?YourLinkLabel”结尾的链接触发更改)。

I ran into the same problem using hashes in links...the page wouldn't reload when the hash tag changed. My solution was to switch the hash tag out for a question mark. That caused links ending with '?YourLinkLabel' to actually reload the page each time the link label changed (the change was triggered by clicking on a link whose href attribute ended with '?YourLinkLabel').

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文