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?

Answers


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 () {}
    });
});

Need Your Help

Swift native base class or NSObject

objective-c swift objective-c-runtime

I tested out some isa swizzling with Swift, and found that it only works when NSObject is a super-class (directly or further up), or by using the '@objc' decoration. Otherwise it will follow a stat...

Hierarchical clustering for bitsequences

algorithm cluster-analysis hamming-distance hierarchical-clustering

This is a homework problem and I'm facing some difficulties to understand it. The home work question is

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.