2010-11-12 95 views
5

我有一個鏈接href爲"/news#1930"。當我在/news頁面上點擊此鏈接時,它不會刷新頁面(它只會將#1930添加到URL的末尾)。我希望它刷新頁面雖然(我使用#標籤後的信息與一些jquery加載相應的iframe中的特定文章,當它不刷新它不會加載新的文章)。有關如何強制刷新的任何想法?如何使用哈希標籤製作頁面Href刷新

我目前的代碼是:

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

回答

-5

散列之前嘗試的空間:

"/news #1930" 
+0

好,良好的通話。這適用於一個鏈接,但我實際上有三個這樣的鏈接。當它們都有空格時,就會出現不刷新的問題。我在第一個,第二個和第三個之間放了一個空格,從而繞開了這個問題。這有效,但不能成爲最好的方式。有關如何使這一點變得更好的想法? – Andrew 2010-11-12 21:43:14

+0

沒有太多的哈希URL。我不認爲有另一條路線。也許標記散列值將是使用多個散列值的好替代。 – 2010-11-14 01:31:07

6

散列是爲了不引起頁面重新加載。如果您使用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傳遞的哈希,並處理頁面內新文章的任何新點擊。

<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> 
+0

該文章位於iframe中,這實際上是造成所有這些問題的原因。在網站上的所有頁面上都有iframe中文章的側邊欄鏈接。我需要一種鏈接到新聞頁面的方法,然後打開正確的文章。我發現這樣做的方式是在側邊欄鏈接中添加一個哈希標記,然後將其轉換爲與文章ID對應的四位數字,並將其附加到iframe網址的末尾。除上述問題外,它適用於所有頁面。如果你對整個問題有更好的解決方法,我肯定會接受。 – Andrew 2010-11-12 22:16:38

+0

在上面的loadArticle()函數中,您可以通知iFrame切換位置(與$('#iframe').attr('src','http://mysite.com/article'+articleId+' .html'); - 除非我不完全瞭解情況,否則你試圖完成的任務應該相當簡單 – 2010-11-14 20:34:20

+0

我確信這應該很容易,但我在解決它時遇到了一些麻煩。我在上面的問題中添加了我目前使用的代碼,它(每個鏈接#標籤之間的空格數量不同)工作得很好,我只知道這不是最好的方法,並且希望做到這一點任何幫助都會很棒。 – Andrew 2010-11-22 15:00:13

-1

試試這個:

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

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

此代碼將重新加載每個頁面女巫環節都有哈希