KeithRules
KeithRules

Reputation: 207

Browser back and forward button events, without a jQuery plugin

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

Answers (1)

Dimitri Vorontzov
Dimitri Vorontzov

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

Related Questions