2014-03-27 64 views
0

我試圖顯示一個確認對話框來顯示一個自定義消息,這將保存或忽略取決於哪個按鈕被單擊的窗體的更改。jQuery - 無法獲得確認對話框出現在Firefox

我有一個部分代碼段,我使用Firefox的工作不再工作。 在beforeunload事件處理程序中使用confirm函數我無法在Firefox中顯示確認對話框,以防止用戶在未接受更改或放棄更改的情況下更改頁面。

我的方法不正確或者這是瀏覽器的問題嗎?

謝謝。

var confirmVal = false; 
    var firstConfirm = false; 

$(window).bind('beforeunload', function() { 
        if (!firstConfirm) { 
         firstConfirm = true; 
         //This is where the confirm dialog should show 
         //It worked in firefox previously but not currently 
         //It still works in Internet Explorer 
         confirmVal = confirm("Save changes? \"OK\" saves changes. \"Cancel\" removes changes."); 
        } 
        if (confirmVal) { 
          // Perform update of database in this block with ajax     
        } 
        else 
        return; 

      }); 
+0

適合我在jsFiddle中使用FF。你確定你有jquery嗎? http://jsfiddle.net/sC38X/ –

+0

謝謝你的鏈接。我不得不在jsfiddle中「運行」代碼來查看對話框。我想確保代碼在「離開」頁面時起作用。這是我的問題。但是這給了我一些信心,我的代碼正在觸發確認。 – Vahe

+0

好吧,所以我嘗試去除當前頁面以外的新頁面,並且對話框出現了。是否還有其他需要重新加載的事件來觸發確認? – Vahe

回答

0

編輯/警告:似乎我的解決方案在Firefox中不起作用,請參閱下面的第一條評論。

你不需要confirm()。 只需在返回中插入一個字符串即可觸發警報。

例:

$(window).on('beforeunload', function() { 
    var e = $.Event('webapp:page:closing'); 
    $(window).trigger(e); // let other modules determine whether to prevent closing 
    if(e.isDefaultPrevented()) { 
     // e.message is optional 
     return e.message || 'You have unsaved stuff. Are you sure to leave?'; 
    } 
}); 

更多的信息在這裏:http://hackab.it/2013/05/page-closing-confirm/

+1

這不適用於Firefox。您無法控制消息。查看https://bugzilla.mozilla.org/show_bug.cgi?id=588292。 – Trenin