ojek
ojek

Reputation: 10068

Html5 - window.onbeforeunload to catch _only_ refresh, back/forward

I would like to use this:

window.onbeforeunload = function () {
    return "u sure?";
}

Only on refresh, forward/back events. Currently this code catches everything. I don't want it to catch link clicks or form post events. Also, I need to have all the code inside of that function, so I was thinking, that maybe there is something like this (pseudocode):

window.onbeforeunload = function () {
    if(it was not a click && it was not a form post)
       return "u sure?";
}

So, reassuming: Is it possible to get the event that caused .onbeforeunload to fire?

Upvotes: 0

Views: 411

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191729

The simplest solution I can think of is to unbind the onbeforeunload event on a form submission or link click.

Array.prototype.forEach.call(document.querySelectorAll('form'), function (elem) {
    elem.addEventListener('submit', function () {
        window.onbeforeunload = function () {}
    });
});

Upvotes: 1

Related Questions