0

First of all I'm not interested of using some jquery plugin. I just want to know how did they achieve it so I can create a script like that.

On html5 history we use window.onpopstate event to detect back/forward browser event. What I want is to create something like this so I can use the hash type of url for other browser that doesn't support html 5

something like this:

if(history.pushState){
   //use html5 history event
   window.onpopstate = function(event){that.__loadCurrentLink();};
}else{
   //use the History event for other browser
   window.historyEvent = function(event){that.__loadCurrentLink();};        
}

Can you guys give some hint or anything so I will have the idea how to do this.

I'm just doing this for learning purposes and for the other people who want to know how to do this too. I hope some javascript monster can lead us the way.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Leysam Rosario
  • 379
  • 2
  • 11
  • 8
    "I just want to know how did they achieve it so I can create a script like that." - Have you considered reading the source of a jquery plugin? And if something doesn't make sense, then post a question about it on SO??? – meder omuraliev Nov 22 '11 at 21:32

3 Answers3

3

Assign an event handler for the hashchange event to window and use a setInterval for older browsers to poll window.location.hash every 100ms etc, so when hashchange isn't supported, you could get pretty much the up to date hash.

I'd also suggest writing your links, buttons, etc to call the function that checks the hash on mouseup, so that you'd be guaranteed almost no delay when you click on a link that changes the hash.

Hashchange support in browsers: caniuse.com

zatatatata
  • 4,761
  • 1
  • 20
  • 41
  • This idea works. But this is not a good idea. becuase if I change the url it will auto navigate because of the setInterval. Even if the link is invalid it will auto navigate so it is not advisable to use this method. – Leysam Rosario Nov 23 '11 at 13:21
  • @LeysamRosario This is how all the libraries do it and this is the only reasonable way (probably the only way altogether). It's up to you to validate the URL and act however you like. You can either display an error, ignore or even auto-complete the URL if it's invalid. And isn't it the whole point that when you change the URL, it fires the handler? There's no way to limit URLs as the user will always be able to change the URL manually. – zatatatata Nov 23 '11 at 14:50
0

There is no event that fires when a "hash change" occurs in IE7, better described in this question.

On - window.location.hash - change?

Since you aren't interested in a "jQuery solution" I recommend that you read the top part of the best answer for IE7.

Community
  • 1
  • 1
Robin Andersson
  • 5,150
  • 3
  • 25
  • 44
0

Many older browsers don't natively support a hash change event. I believe the way jQuery abstracts this functionality into their event listeners is by using a setInterval() to constantly poll for hash changes.

mattacular
  • 1,849
  • 13
  • 17