[英]how to detect back button of browser in jquery and if any dialogue is open close it?
我想要做的是,如果在打开任何其他弹出窗口时按返回按钮,则应该关闭该弹出窗口并重新加载页面。
我在下面尝试过。 但只使用键盘后退按钮,而不使用浏览器后退按钮。 谁能帮我解决这个问题
$(document).ready(function() {
$(document).bind("keydown keypress", function(e){
if( e.which == 8 ){ // 8 == backspace
alert("back button");
}
});
}
用于检测浏览器后退并关闭弹出窗口的代码段。
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);
});
如果您希望它停止导航至此:
$(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();
}
});
对我来说,它捕获并禁用了浏览器。 您可以使用postMessage
在Windows之间进行通信
有关在Windows之间进行通信的更多信息。 https://davidwalsh.name/window-iframe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.