Javascript 单击浏览器后退按钮时触发事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4827411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
trigger event on browser back button click
提问by donk
How do I listen for a browser back button click and call a function that I've defined and disable the normal behaviour of the back button? I'm using jQuery.
如何侦听浏览器后退按钮单击并调用我定义的函数并禁用后退按钮的正常行为?我正在使用 jQuery。
采纳答案by Caspar Kleijne
jQuery history pluginhelps you to support back/forward buttons and bookmarks in your javascript applications. You can store the application state into URL hash and restore the state from it.
jQuery 历史插件可帮助您在 javascript 应用程序中支持后退/前进按钮和书签。您可以将应用程序状态存储到 URL 哈希中并从中恢复状态。
回答by Poelinca Dorin
You could play with :
你可以玩:
$(window).unload( function () { alert("Bye now!"); } );
$(window).unload( function () { alert("Bye now!"); } );
But this will be trigered when the user clicks a link that goes to another page , types a new address in the address bar or ... basicly it will be trigered whenever the user leaves the current page . I don't think you can stop the user from going away from you're page tough .
但是当用户点击一个链接到另一个页面时,这将被触发,在地址栏中输入一个新地址或者...基本上只要用户离开当前页面就会被触发。我不认为你可以阻止用户离开你的页面。
UPDATE:
更新:
unload() is now deprecated from jquery 1.8
unload() 现在已从 jquery 1.8 中弃用