window.onbeforeunload = function(evt) {
var message = 'Are you sure you want to leave the page. All data will be lost!';
if (typeof evt === 'undefined') {
evt = window.event;
}
if (evt && !($("#a_exit").click)) {
evt.returnValue = message;
}
return message;
};
我希望用戶離開頁面點擊鏈接(僅有id ="a_exit"
)。在其他情況下,例如刷新頁面,點擊另一個鏈接,將提示用戶如果他/她想離開頁面。我試圖使用上面的代碼。當我點擊退出鏈接時,它仍然問我是否想離開。在頁面上有條件的onbeforeunload事件
我猜測它不會工作,但在最後一個鉻,IE和FF下測試,它的工作原理http://jsfiddle.net/Suf99/ –