-
-
Save louisremi/1326553 to your computer and use it in GitHub Desktop.
popstate, polyfill that executes window.onpopstate callback whenever location.hash changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function( currHash ){ | |
// feature detection + setInterval based polyfill | |
history.pushState || function checkHash( init ) { | |
// save current hash for next check when check if hash has changed | |
currHash != ( currHash = location.hash ) | |
// and vars are not being initialized | |
&& !init | |
// and callback exists | |
&& ( init = self.onpopstate ) | |
// then execute onpopstate callback | |
&& init(); | |
// schedule next check | |
setTimeout( checkHash, 300 ) | |
// init | |
}(1) | |
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function(a){history.pushState||function f(b){a!=(a=location.hash)&&!b&&(b=self.onpopstate)&&b();setTimeout(f,300)}(1)}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 @louis_remi <http://louisremi.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "popstate", | |
"description": "polyfill that executes window.onpopstate callback whenever location.hash changes", | |
"contributors": [ | |
"louisremi", | |
"tsaniel", | |
"atk" | |
], | |
"keywords": [ | |
"polyfill", | |
"history", | |
"DOM", | |
"onpopstate" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Current hash: <b id="currHash">undefined</b></div> | |
<ul> | |
<li><a href="#first hash">first link</a></li> | |
<li><a href="#second hash">second link</a></li> | |
<li><a href="#third hash">third link</a></li> | |
</ul> | |
<script> | |
!function(a){history.pushState||function f(b){a!=(a=location.hash)&&!b&&(b=self.onpopstate)&&b();setTimeout(f,300)}(1)}() | |
window.onpopstate = function() { | |
document.getElementById("currHash").innerHTML = location.hash; | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, thanks for the reminder