admin管理员组

文章数量:1310455

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?

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?

Share Improve this question edited Jun 12, 2013 at 17:49 KeithRules asked Jun 12, 2013 at 17:11 KeithRulesKeithRules 2071 gold badge4 silver badges15 bronze badges 3
  • No. You're looking for the HTML5 history API. – SLaks Commented Jun 12, 2013 at 17:12
  • 1 It has already been answered here: stackoverflow./questions/4840457/… – Antoine Commented Jun 12, 2013 at 17:15
  • Modified the question to clarify – KeithRules Commented Jun 12, 2013 at 17:19
Add a ment  | 

1 Answer 1

Reset to default 7
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

本文标签: javascriptBrowser back and forward button eventswithout a jQuery pluginStack Overflow