如何使用哈希标记Href刷新页面

我有一个href为"/news#1930" 。 当我在/news页面上点击此链接时,它不刷新页面(它只是将#1930添加到URL的末尾)。 我希望它刷新页面(我使用#tag后面的信息和一些jquery在相应的iframe中加载特定的文章,当它不刷新时不会加载新文章)。 关于如何强制刷新的任何想法?

我目前的代码是:

 $(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); } }); 

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

 $(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传递的哈希值,并处理页面中新文章的任何新点击。

   Article Viewer         
  $("a").click(function () { var hash = this.hash; if (hash != "" || hash!= null) window.location.reload(); }); 

试试这个:

 $("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(); } }); }); 

在哈希之前尝试一个空格:

 "/news #1930"