2009-08-24 98 views
1

我有這樣的代碼塊:JQuery的DOM操作和window.onbeforeunload在Internet Explorer

window.onbeforeunload = function(){ 
      <% if (Session["DisableExitConfirmation"] == null || !(bool)Session["DisableExitConfirmation"]) { %> 
       if (isDirty) return '<%= Html.EncodeJsString(Html.Resource("profile_unsavedchanges")) %>'; 
      <% } else { %> 
       if (isDirty) alert('<%= Html.EncodeJsString(Html.Resource("profile_unsavedchanges")) %>'); 
      <% } %> 
     }; 

我做的一些頁面的DOM操作,添加Li元素到UL。當我這樣做時,我將isDitry設置爲true,當我在Internet Explorer中時,會引發window.onbeforeunload。我希望只在用戶刷新頁面或在isDirty爲true時退出它纔會引發它。

在Firefox中我沒有這種行爲。

我不明白爲什麼當我用JQuery做DOM操作時會引發這個事件。

有沒有人知道這個解決方法?

非常感謝,

查爾斯

回答

1

試試這個, 「onunload的」 應該FF工作。

<body onunload="s();" onbeforeunload="s();">