【问题标题】:Override Window.onbeforeOnLoad Message and perform some action on Stay Page and Leave Page?覆盖 Window.onbeforeOnLoad 消息并对停留页面和离开页面执行一些操作?
【发布时间】:2016-11-18 14:56:29
【问题描述】:

对于 Firefox 和 Chrome,我试图覆盖浏览器为 onbeforeOnLoad 函数显示的默认消息。 以下是我发现的问题

  1. 即使我竭尽全力,我也无法覆盖该消息。
  2. 我根本无法捕捉停留页面或离开页面上的事件。我想执行一些操作,例如,如果是停留页面,请执行此操作,如果是离开页面,请执行此操作。它甚至不显示事件的警报消息。

请找到我一直在尝试的代码:

window.onbeforeunload = function(e) 
{
    alert(e);
    var myMessage = 'Hello.';
    e.returnValue = myMessage;
    return dialogText;

 };

也试过了:

window.onbeforeunload = function() {
    return 'Hello!';
}

【问题讨论】:

标签: javascript jquery google-chrome firefox onbeforeunload


【解决方案1】:

other stack overflow post

答案就是您不能覆盖 onbeforeUnLoad 消息。

但是,您可以对消息执行某些操作,例如:

    $(document).ready(function()
{

    $(window).on("beforeunload", windowBeforeUnload);

    $("#reeditbuttonjs").on("mouseenter", stopPopup)
            .on("mouseout", function() 
    {
        $(window).on("beforeunload", windowBeforeUnload);
    });
});


function stopPopup()
{
    $(window).off("beforeunload");
}


function windowBeforeUnload()
{
    return "You have unsaved data!";
}

该函数运行一次后会关闭unbeforeunload函数。

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    相关资源
    最近更新 更多