0

For both Firefox and Chrome, I am trying to override the default message that is displayed by the browser for onbeforeOnLoad function. Below are the issues i found

  1. I was not able to override the message even though i tried my heart out.
  2. I am not at all able to capture the event on Stay Page or Leave Page. I want to perform actions like if it is Stay Page Do This and if it is Leave Page do This.It is not even displaying the alert message for the event.

Please find the code i have been trying:

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

 };

Tried this as well:

window.onbeforeunload = function() {
    return 'Hello!';
}
sTg
  • 4,313
  • 16
  • 68
  • 115
  • Possible duplicate of [How can I override the OnBeforeUnload dialog and replace it with my own?](http://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own) – Heretic Monkey Nov 18 '16 at 15:10
  • You can check the explanation I gave here (with examples regarding each of the browsers): http://stackoverflow.com/questions/38879742/is-it-possible-to-display-a-custom-message-in-the-beforeunload-popup/38880926#38880926 – Dekel Nov 20 '16 at 16:09

1 Answers1

0

see other stack overflow post

The answer simply is you cannot override the onbeforeUnLoad message.

You can however do something with the message, for example:

    $(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!";
}

This function would turn off the unbeforeunload function when it has run once.

Community
  • 1
  • 1
Fondas
  • 36
  • 7