Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make Page with Hash Tag Href Refresh

I have a link that has an href of "/news#1930". When I click on this link when on the /news page, it does not refresh the page (it just adds #1930 to the end of the URL). I would like it to refresh the page though (I am using the info after the # tag with some jquery to load that specific article in a corresponding iframe and when it doesn't refresh it won't load the new article). Any ideas on how to force the refresh?

The code I have currently is:

$(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);
        }
 });
like image 218
Andrew Avatar asked Nov 15 '22 05:11

Andrew


2 Answers

Hashes are meant to not cause a page reload. If you're using jQuery to load that article you may want to perform a history.go(0) or window.location.reload, then use jQuery to find the hash tag and process (on the load event of the refreshed page).

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

EDIT I assume you're going the hash route for the same reason websites like facebook do as well--for book-mark ability and indexing despite that you're using ajax for the seamless integration.

EDIT 2 Here is what I've come up with to show what I'm referring to. This will load up a hash that's been passed through the URL and handle any new clicks to new articles within the page.

<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>
like image 172
Brad Christie Avatar answered Dec 14 '22 23:12

Brad Christie


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

This code will reload every page witch link has hash

like image 41
Cybertron Avatar answered Dec 14 '22 23:12

Cybertron