在不使用任何外部插件的情况下检测 javascript/jquery 中的浏览器后退按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25801410/
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
detecting browser back button in javascript/jquery without using any external plugins
提问by SivaRajini
I want to detect browser back button in java-script/jquery without using any external plugins.
我想在不使用任何外部插件的情况下检测 java-script/jquery 中的浏览器后退按钮。
I am going to support following browsers
我将支持以下浏览器
IE8,IE9,FireFox,Chrome
IE8、IE9、火狐、Chrome
I googled so many links and found below part of code from code project
我在谷歌上搜索了很多链接,发现下面是代码项目中的部分代码
<body onbeforeunload=”HandleBackFunctionality()”>
function HandleBackFunctionality()
{
if(window.event)
{
if(window.event.clientX < 40 && window.event.clientY < 0)
{
alert("Browser back button is clicked...");
}
else
{
alert("Browser refresh button is clicked...");
}
}
else
{
if(event.currentTarget.performance.navigation.type == 1)
{
alert("Browser refresh button is clicked...");
}
if(event.currentTarget.performance.navigation.type == 2)
{
alert("Browser back button is clicked...");
}
}
}
The above code working fine IE browser. that means i can able to get the window.event.clientX and clientY values in IE browser. but in chrome/firefox not working.
上面的代码在 IE 浏览器中运行良好。这意味着我可以在 IE 浏览器中获取 window.event.clientX 和 clientY 值。但在 chrome/firefox 中不起作用。
When browser back button is clicked, i need to refresh the page.
单击浏览器后退按钮时,我需要刷新页面。
how can i detect browser back button click event in all browsers (IE,firefox,chrome)
如何在所有浏览器(IE、firefox、chrome)中检测浏览器后退按钮单击事件
any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by Hamix
That can be simply dropped into your web page, and when the user clicks back, it will call a function. The default function on this call is a javascript alert “Back Button Clicked”.
这可以简单地放入您的网页中,当用户点击返回时,它会调用一个函数。此调用的默认函数是一个 javascript 警报“Back Button Clicked”。
To replace this functionality, you simply need to override the OnBack function. This can be done by using the code below.
要替换此功能,您只需覆盖 OnBack 函数。这可以通过使用下面的代码来完成。
<script type="text/javascript">
bajb_backdetect.OnBack = function()
{
alert('You clicked it!');
}
</script>
This will now replace the “Back Button Clicked” alert with a “You clicked it!'” alert.
support following browsers
IE8,IE9,FireFox,Chrome
Browser Back Button Detection
Or using jquery
Catching browser back
Using standard javascript
Mastering The Back Button With Javascript
现在,这将用“您单击了它!”警报替换“单击后退按钮”警报。
支持以下浏览器 IE8、IE9、FireFox、Chrome
浏览器后退按钮检测
或使用jquery
捕获浏览器返回
使用标准javascript
掌握后退按钮使用 Javascript
回答by Vivin Bangera
Since you are supporting IE 8 and 9 as well, there isnt a single method that will help you solve this. You could use the history API of html5 but that doesn't support IE8 and 9. As mysteryos mentioned in the above comment, you could use window.onPopState for chrome, firefox etc, and for IE you could create a separate js file and use conditional statements to fire it when the user uses IE.
由于您也支持 IE 8 和 9,因此没有一种方法可以帮助您解决此问题。您可以使用 html5 的历史 API,但它不支持 IE8 和 9。正如上面评论中提到的神秘主义,您可以将 window.onPopState 用于 chrome、firefox 等,对于 IE,您可以创建一个单独的 js 文件并使用当用户使用 IE 时触发它的条件语句。
For example:
例如:
<!--[if lte IE 9]>
<script src="youIESpecificfile.js"></script>
<![endif]-->
Read more about the HTML5 History API here: onPopState by mozilla
在此处阅读有关 HTML5 History API 的更多信息:mozilla 的 onPopState
Hope this helps!
希望这可以帮助!

