Is it possible to display a custom message in the beforeunload popup?

后端 未结 5 2380
眼角桃花
眼角桃花 2020-11-22 06:30

When using window.onbeforeunload (or $(window).on(\"beforeonload\")), is it possible to display a custom message in that popup?

Maybe a sma

5条回答
  •  萌比男神i
    2020-11-22 07:31

    I just made a div appear that shows a message in the background. It is behind the modal but this is better then nothing. It is kind of shady but at least you can give your user some info on why you bother her/him not to leave.

    constructor($elem)
    {
        $(window).unbind().bind('beforeunload', (e) => this.beforeUnload(e));
    }
    beforeUnload(e)
    {
        $('#leaveWarning').show();
        setTimeout(function(){
            $('#leaveWarning').hide();
        }, 1); // set this to 1ms .. since timers are stopped for this modal,
               // the message will disappear right after the user clicked one of the options  
        return "This message is not relevant in most modern browsers.";
    }
    

    Here is a working Fiddle https://jsfiddle.net/sy3fda05/2/

提交回复
热议问题