How to define different messages when Navigation Away and Reloading page?

18 views Asked by At

I want a simple thing, but I am not getting it to work

Just like the browser change the confirmation text from

"Are you sure you want to leave this page?"

to

"Are you sure you want to reload this page?"

depending what you are doing, I want that my custom confirmation text also knows which action the user is performing.

Currently I have this:

window.onbeforeunload = function (event) {
    var infoText = (event.srcElement == event.target) ? "Reloading" : "Movig from";
    infoText += " this page will cause you to lose current information.";
    return infoText;
};

I thought that comparing the event.srcElement vs. event.target will be enough as they have the navigation links. But that if statement always returns true, so it always looks like it is reloading even if the destination link is different.

As the browser knows, I thinks there must be some way to do it. If not, I'll have to create a generic message. (Obviously I could make it generic and... done. But now I really want to know it.)

0

There are 0 answers