[英]Conditional onbeforeunload event in a page
window.onbeforeunload = function(evt) {
var message = 'Are you sure you want to leave the page. All data will be lost!';
if (typeof evt === 'undefined') {
evt = window.event;
}
if (evt && !($("#a_exit").click)) {
evt.returnValue = message;
}
return message;
};
I want user to leave the page clicking to the link (has id ="a_exit"
) only.我希望用户只点击链接(有
id ="a_exit"
)离开页面。 In other circumstances such as refreshing the page, clicking another link, user will be prompted that if he/she wants to leave the page.在其他情况下,如刷新页面,点击其他链接,会提示用户是否要离开页面。 I have tried to use the code above.
我曾尝试使用上面的代码。 It still asks me if I want to go away when I click the exit link.
当我单击退出链接时,它仍然询问我是否要离开。
$(window).bind('beforeunload',function() {
return "'Are you sure you want to leave the page. All data will be lost!";
});
$('#a_exit').live('click',function() {
$(window).unbind('beforeunload');
});
Above works for me以上对我有用
It will always prompt you if you want to leave the page.它会一直提示您是否要离开该页面。 It's a security issue and cannot be worked-around .
这是一个安全问题,无法解决。
Moreover, anything you return in the onbeforeunload
event handler that is not void
will be treated as the message for the prompt.此外,您在
onbeforeunload
事件处理程序中返回的任何非void
都将被视为提示消息。 Refer to this article: https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload参考这篇文章: https : //developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload
My quick example of the conditional prompt before leaving page:我离开页面前的条件提示的快速示例:
window.onbeforeunload = confirmExit;
function confirmExit(event) {
var messageText = tinymce.get('mMessageBody').getContent();
messageText = messageText.trim();
// ... whatever you want
if (messageText != "")
return true;
else
return void (0);
};
It works under Chrome, FF.它在 Chrome、FF 下工作。
This worked for me.这对我有用。
window.onbeforeunload = function(){
if(someBoolean) {
return 'message'
}else{
window.onbeforeunload = null;
}
}
您可以在点击处理程序中删除window.onbeforeunload
。
You can return null
when you do not want the prompt to show.当您不想显示提示时,您可以返回
null
。 Tested on Chrome 79.在 Chrome 79 上测试。
window.onbeforeunload = () => {
if(shouldPrompt){
return true
}else{
return null
}
}
BTW modern browser no longer support custom onBeforeUnload promp text.顺便说一句,现代浏览器不再支持自定义 onBeforeUnload 提示文本。
How about a variable to decide, if the link was clicked?如果链接被点击,如何决定一个变量?
var exitClicked = false;
$("#a_exit").click(function() {
exitClicked = true;
});
window.onbeforeunload = function(evt) {
//...
if(evt && !exitClicked) {
evt.returnValue = message;
}
//...
};
You only set exitClicked = true
when the link was really clicked and afterwards, before unloading you can simply check this variable.你只在链接被真正点击时设置
exitClicked = true
,之后在卸载之前你可以简单地检查这个变量。
This is quite old but I wanted just to change the code given that .live is no longer working in Jquery.这已经很老了,但我只想更改代码,因为 .live 不再在 Jquery 中工作。 So, the updated version would be:
所以,更新的版本将是:
$(window).bind('beforeunload',function() {
return "'Are you sure you want to leave the page. All data will be lost!";
});
$('#a_exit').on("click", function(){
$(window).unbind('beforeunload');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.