Reputation: 207
I want to be able to add certain behavior when a user clicks browser "back" and "forward" buttons.
Is there a way to say with JavaScript something like this:
backButton.onclick = function() {
// do something
}
forwardButton.onclick = function() {
//do something else
}
Is there a way to do that directly with JavaScript, without relying on any plugin?
Upvotes: 3
Views: 10861
Reputation: 8104
window.addEventListener("popstate", function(e) { // if a back or forward button is clicked
// do whatever
}
Works only in HTML5-enabled browsers, though.
For other browsers support, look into History.js
Upvotes: 7