![](/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.