Created
March 18, 2018 02:38
-
-
Save sean3z/037b0ebbdeb8912d6a1efe1e081c7a4c to your computer and use it in GitHub Desktop.
pushState and popstate example using jQuery
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
// when a user clicks one of our links | |
$('a').click(function (e) { | |
// Detect if pushState is available | |
if (history.pushState) { | |
history.pushState(null, null, $(this).attr('data-location')); // URL is now /inbox/N | |
// showMailItem(); // example function to do something based on link clicked | |
} | |
return false; | |
}); | |
// Used to detect initial (useless) popstate. | |
// If history.state exists, assume browser isn't going to fire initial popstate. | |
var popped = ('state' in window.history && window.history.state !== null), initialURL = location.href; | |
// listen to popstate events | |
$(window).bind('popstate', function (event) { | |
// Ignore inital popstate that some browsers fire on page load | |
var initialPop = !popped && location.href == initialURL | |
popped = true | |
if (initialPop) return; | |
// showMailOverview(); // exmaple function to do something when user clicks 'Back' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment