首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Hash标签Href刷新页面

如何使用Hash标签Href刷新页面
EN

Stack Overflow用户
提问于 2010-11-13 05:06:58
回答 4查看 13.2K关注 0票数 5

我有一个链接,它有一个"/news#1930"的href。当我在/news页面上单击这个链接时,它不会刷新页面(它只是将#1930添加到URL的末尾)。我希望它刷新页面(我正在使用#标签之后的信息和一些jquery来加载相应的iframe中的特定文章,当它不刷新时,它不会加载新文章)。对如何强制刷新有什么想法吗?

我目前拥有的代码是:

代码语言:javascript
复制
$(document).ready(function() {
  var $news_story       = $(window.location.hash.substring(1)),
      $news_number      = $news_story.selector,
      $news_url         = 'http://www.synergy-pr.com/news/mediacenter/index.html?view=article&CustomerID=41b1if9-a17d4va5sf7of15e3kb0pa3kd2y-99d03n8sq4ad2xk8h47j00r98el5pf&ClientArticleID=' + $news_number, //url of specific article
      $news_url_default = 'http://www.synergy-pr.com/news/mediacenter/index.html?CustomerID=41b1if9-a1hd4xa52f78f1ke3bb0oa3ld2k-90208c8g64x02nh8wf7ad0m181p5su'; //url of general news page

        if ($news_number.length>0) { //if their is a # with a number, this means it is a sidebar link and should load that specific article
          $('#news-feed').attr('src', $news_url);
        } else { //if not, load the main news page
          $('#news-feed').attr('src', $news_url_default);
        }
 });
EN

回答 4

Stack Overflow用户

发布于 2010-11-13 05:21:36

散列不会导致页面重新加载。如果使用jQuery加载该文章,可能需要执行history.go(0)或window.location.reload,然后使用jQuery查找散列标记和进程(在刷新页面的load事件上)。

代码语言:javascript
复制
$(document).ready(function(){
  loadArticle = function(articleId){
    // code to query/load the article
  };
  if (window.location.hash)
    articleLoad(window.location.hash);

  $('.articleLink').click(function(){
    window.location.hash = $(this).attr('rel');
    window.location.reload();
  });
});

编辑我认为你走哈希路和facebook这样的网站是出于同样的原因--为了书签和索引,尽管你使用ajax进行无缝集成。

2这里是我用来展示我所指的东西。这将加载一个通过URL传递的散列,并处理页面中任何新文章的新点击。

代码语言:javascript
复制
<html>
  <head>
    <title>Article Viewer</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){

        // loader function to serve up the correct article (pushes the article number to an iFrame
        // so it can remotely load the article within the page
        loadArticle = function(articleId){
          // set the hash (incase it's called from a link instead of a page load)
          window.location.hash = articleId;
          // change the iFrame src to the actual path fo the article page (customize this to fit your needs)
          $('#article-frame').attr('src','view_article.php?article='+articleId);
        };

        // check for a hash (#????) tag in the URL and load it (to allow for bookmarking)
        if (window.location.hash)
          loadArticle(window.location.hash.substring(1));

        // attack a click event to all the links that are related to loading an article
        $('.article-link').click(function(){
          // grab raw article id from rel tag
          var articleId = $(this).attr('rel');

          // shove loading it off to the "loader"
          loadArticle(articleId);

          // cancel the navigation of the link
          return false;
        });
      });
    </script>
  </head>

  <body>
    <ul>
      <?php for ($a = 1; $a < 10; $a++) echo "<li><a href=\"view_article.php?article={$a}\" rel=\"{$a}\" class=\"article-link\">View Article {$a}</a></li>\r\n      "; ?>
    </ul>
    <iframe id="article-frame" src="view_article.php?article=0" width="640" height="480">
      This page uses frames, unfortunately your browser does not support them.
    </iframe>
  </body>
</html>
票数 6
EN

Stack Overflow用户

发布于 2015-07-02 15:35:36

代码语言:javascript
复制
    $("a").click(function () {
        var hash = this.hash;
        if (hash != "" || hash!= null)
            window.location.reload();
    });

此代码将重新加载每个链接有散列的页面

票数 1
EN

Stack Overflow用户

发布于 2013-02-15 23:19:39

试试这个:

代码语言:javascript
复制
$("a").click(function(){
    $("a").click(function(e){

    var location = $(window).attr('location').pathname.substring(1);
    var goingToPage = $(this).attr('href');

    // if goingToPage is same page as current page, then assign new url and reload
    // this is required to reload the page on a hash tag
    if(goingToPage.indexOf(location) != -1){
        $(window).attr('location').assign(goingToPage);
        $(window).attr("location").reload();
    }
});
});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4169037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档