open a custom popup on browser window/tab close

前端 未结 6 804
忘掉有多难
忘掉有多难 2020-12-02 00:30

I am trying to open a custom popup on browser window/tab close.

In details if a user clicks on the browser window/tab close button a custom popup will appear with s

6条回答
  •  孤城傲影
    2020-12-02 01:19

    The feature you're talking about is the window.onbeforeunload event, and unfortunately the only way it can be customized is to provide a custom string message for the user, because the potential for abuse is so high.

    The api reference over at msdn for Internet Explorer states:

    The default statement that appears in the dialog box, "Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.", cannot be removed or altered.
    

    I take this to mean the dialog itself cannot be altered, all you can do is provide an additional context-specific message. I cant locate the same reference for Chrome, but it seems to be the same story there.

    window.onbeforeunload = function() {
        //all you can do is provide a message..
        return "you have unsaved changes, if you leave they will be lost";
    }
    

提交回复
热议问题