Detect change of location.hash?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akdb8r
    New Member
    • Jun 2006
    • 2

    #1

    Detect change of location.hash?

    Is there a way for JavaScript to detect when the user changes the URL hash (location.hash) ?

    Thanks in advance! :)
  • akdb8r
    New Member
    • Jun 2006
    • 2

    #2
    Anybody? Anybody? Bueller?

    Thanks again! :)

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Anybody? Anybody?

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by akdb8r
        Is there a way for JavaScript to detect when the user changes the URL hash (location.hash) ?

        Thanks in advance! :)
        Use a Timer with the optimum interval time.
        Code:
        var curHashVal = window.location.hash; //global reference
        window.onload = function(){ //call the function at onLoad time.
         setInterval(1000,function(){
          if(curHashVal!=window.location.hash){ //changed
           //do some work then save the current hash value
           curHashVal = window.location.hash;
          }
         });
        }

        Comment

        • akadeco
          New Member
          • Aug 2009
          • 7

          #5
          you got the time and callback around the wrong way

          Comment

          Working...