onBeforeUnload handler says null in IE

前端 未结 2 1089
深忆病人
深忆病人 2021-01-01 20:34

my web-app has this:

$(window).bind(\'beforeunload\', function() {
    if(unSavedChanges == true)
    {
        return \'You have unsaved changes\';
    }
           


        
相关标签:
2条回答
  • 2021-01-01 20:41

    I use normal javascript for this and works fine

    function setConfirmUnload(on) {
         window.onbeforeunload = (on) ? unloadMessage : null;
    }
    
    function unloadMessage() {
         return 'Please stay on the page';
    }
    
    0 讨论(0)
  • 2021-01-01 21:01

    Remove the return null line and it should be fine (in Javascript null and undefined are different things).

    By the way, MDN says that for maximum compatibility you should be accepting an event parameter and setting event.returnValue as well.

    0 讨论(0)
提交回复
热议问题