您正在討論的功能是window.onbeforeunload事件,不幸的是,它可以自定義的唯一方法是爲用戶提供自定義字符串消息,因爲濫用的可能性非常高。
在MSDN上的Internet Explorer的API參考了規定:
The default statement that appears in the dialog box, "Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.", cannot be removed or altered.
我認爲這意味着該對話框本身不能改變,所有你能做的就是提供一個附加的特定上下文信息。我無法找到Chrome的相同參考,但它似乎是同一個故事。
window.onbeforeunload = function() {
//all you can do is provide a message..
return "you have unsaved changes, if you leave they will be lost";
}
不,不,不這樣做,這是一個可怕的用戶體驗。如果用戶點擊關閉該選項卡,用戶希望該選項卡關閉,並顯示一條消息提醒垃圾網站。 – elclanrs
感謝您的寶貴話語......但暫時我想實現它的一個重要原因。 – Mufeed