[英]Using Confirm() in onbeforeunload() event
我想在卸載頁面之前出現一個確認框,如果用戶單擊確定,則需要在離開頁面之前調用一個函數,如果他點擊取消它應該保持在同一頁面上。
window.onbeforeunload = function() {
if (!submitFormOkay) {
if (confirm("Are you sure you want to leave?")) {
leavepage();
} else {
submitfromokay = true;
location.reload();
}
}
}
無論如何我可以使用JS來完成這項工作嗎?
不,你不能。 以某種方式實現該目標的唯一方法是從onbeforeunload
處理程序返回String onbeforeunload
。 某些瀏覽器(例如Chrome )將使用該String並將其顯示在默認警告消息中。 但是其他瀏覽器不會使用它而只顯示標准消息,這會讓用戶有機會中斷頁面卸載。
// Chrome will use the 'Y U LEAVING' string in its warning message now
window.onbeforeunload = function() {
if(!submitFormOkay) {
return 'Y U LEAVING ?';
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.