[英]how to detect back button of browser in jquery and if any dialogue is open close it?
What i want to do is, if i press back button when any other pop up is opened , i should close that pop up and reload the page . 我想要做的是,如果在打开任何其他弹出窗口时按返回按钮,则应该关闭该弹出窗口并重新加载页面。
i have tried below. 我在下面尝试过。 but it is only taking the keyboard backbutton but not browser back button.
但只使用键盘后退按钮,而不使用浏览器后退按钮。 can any one help me to solve this
谁能帮我解决这个问题
$(document).ready(function() {
$(document).bind("keydown keypress", function(e){
if( e.which == 8 ){ // 8 == backspace
alert("back button");
}
});
}
Code snippet for detecting browser back and closing the popup. 用于检测浏览器后退并关闭弹出窗口的代码段。
var path = 'pageURL';
history.pushState(null, null, path + window.location.search);
window.addEventListener('popstate', function (event) {
//Code to close the pop up
history.pushState(null, null, path + window.location.search);
});
If you want it to stop navigating to this: 如果您希望它停止导航至此:
$(document).bind("keydown", function(e) {
// if a object has focus
// inputs, textarea, etc.
if($(":focus").length != 0) {
return;
}
// else don't allow the press
if(e.keyCode == 8) {
parent.postMessage("message");
window.close();
e.preventDefault();
}
});
For me it captures and disables the browser back. 对我来说,它捕获并禁用了浏览器。 And you can comunicate between windows with
postMessage
您可以使用
postMessage
在Windows之间进行通信
More info on communicating between windows. 有关在Windows之间进行通信的更多信息。 https://davidwalsh.name/window-iframe
https://davidwalsh.name/window-iframe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.