3 回答

TA貢獻1805條經驗 獲得超9個贊
onbeforeunload是你想要的那個 您的函數“應將字符串值分配給Event對象的returnValue屬性并返回相同的字符串”。查看Microsoft和Mozilla的文檔以了解詳細信息。
瀏覽器將使用您返回的字符串為用戶提供一個自定義確認框,從而允許他們拒絕選擇停留在那的用戶。必須采取這種方式來防止惡意腳本引起“瀏覽器拒絕”攻擊。

TA貢獻2051條經驗 獲得超10個贊
此代碼按照Natalie的建議發出警告,但如果提交了頁面上的表單,則會禁用警告。使用JQuery。
var warning = true;
window.onbeforeunload = function() {
if (warning) {
return "You have made changes on this page that you have not yet confirmed. If you navigate away from this page you will lose your unsaved changes";
}
}
$('form').submit(function() {
window.onbeforeunload = null;
});

TA貢獻1111條經驗 獲得超0個贊
window.onbeforeunload = function() {
if (warning) {
return 'You have made changes on this page that you have not yet confirmed.
If you navigate away from this page you will loose your unsaved changes';
}
}
Chrome,Safari和Opera不支持
添加回答
舉報