Alerts when navigating away from a web page

后端 未结 2 1815
死守一世寂寞
死守一世寂寞 2020-11-30 21:40

When I try to close my Google docs tab with unsaved changes, this is what I get in my browser (FF 3.5).

Are you sure you want to navigate away from

2条回答
  •  囚心锁ツ
    2020-11-30 21:53

    By the browser. It's the beforeunload event handler that returns the customized text of the dialog, which is only the middle of the three paragraphs - the other two paragraphs as well as the text of the buttons cannot be customized or otherwise changed.

    window.onbeforeunload = function(){ return 'Testing...' }
    
    // OR
    
    var unloadListener = function(){ return 'Testing...' };
    window.addEventListener('beforeunload', unloadListener);
    

    Will yield a dialog that says

    Are you sure you want to navigate away from this page?
    
    Testing...
    
    Press OK to continue, or Cancel to stay on the current page.
    

    You can nullify this by setting the handler to null

    window.onbeforeunload = null;
    
    // OR
    
    window.removeEventListener('beforeunload', unloadListener);
    

提交回复
热议问题