![](/img/trans.png)
[英]how to open a different page when cancel button is clicked in the Confirm dialog?
[英]Making the leaving site confirm dialog not showing up when a button on the page is clicked
我最近在页面中添加了一个JavaScript对话框,该对话框可让用户确认他们是否真的想离开页面。 但是,我还有一些导航到页面上其他页面的按钮,并且我不希望当用户单击这些按钮时显示离开站点确认对话框。
我还没有真正找到解决方法,部分原因是我是HTML和JavaScript的新手。
以下是我用于确认对话框的代码,我也是从此处通过Stack Overflow获取的。
window.onbeforeunload = function (e) {
e = e || window.event;
if (e) { // For IE and Firefox prior to version 4
e.returnValue = 'Any string';
}
return 'Any string'; // For Safari
};
您可以使用全局变量来控制导航方式。
这是一个示例,我使用一个名为needConfirm
的变量来处理网页是否显示确认消息。
var needConfirm = true;
window.onbeforeunload = function (e) {
if (!needConfirm) {
return; // do nothing, just redirect...
}
e = e || window.event;
if (e) { // For IE and Firefox prior to version 4
e.returnValue = 'Any string';
}
return 'Any string'; // For Safari
};
// function to change needConfirm value to `false`
function disableConfirm() {
needConfirm = false;
}
调用disableConfirm()onClick获取要允许的按钮和链接。 例如
<a href="/other_page_url" onClick="disableConfirm()">Redirect without confirmation...</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.