我有一個系統,我想檢查用戶,如果他們確定他們想要在設置髒標誌後離開頁面。在Chrome和IE中使用jQuery在body元素上設置onbeforeunload
我正在使用下面的代碼 - 在FireFox中,我可以通過FireBug查看頁面源,並且標記正確地插入了onbeforeunload屬性。
在Chrome和FireFox中,雖然沒有發生這種情況,但我可以在沒有任何警告的情況下離開頁面。用於更新body標籤的jQuery行肯定正在執行,它只是沒有執行它。
if ($("body").attr('onbeforeunload') == null) {
if (window.event) {
// IE and Chrome use this
$("body").attr('onbeforeunload', 'CatchLeavePage(event)');
}
else {
// Firefox uses this
$("body").attr('onbeforeunload', 'return false;CatchLeavePage(event)');
}
}
任何想法如何從這裏開始?
我能夠使用 $( 「身體」)的CSS( 「保證金」, 「50像素」)。 和 $(「body」)。attr(「test」,「hello」); 看來我現在無法設置onbeforeunload屬性... – Graeme 2009-11-26 10:49:55