How can I override the OnBeforeUnload dialog and replace it with my own?

前端 未结 11 2045
一生所求
一生所求 2020-11-21 06:14

I need to warn users about unsaved changes before they leave a page (a pretty common problem).

window.onbeforeunload=handler

This works bu

11条回答
  •  忘掉有多难
    2020-11-21 07:06

    What worked for me, using jQuery and tested in IE8, Chrome and Firefox, is:

    $(window).bind("beforeunload",function(event) {
        if(hasChanged) return "You have unsaved changes";
    });
    

    It is important not to return anything if no prompt is required as there are differences between IE and other browser behaviours here.

提交回复
热议问题