Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all window events in JQuery 1.9?

I have recently upgraded JQuery to version 1.9. On version 1.4, I was doing something like this:

     var windowEvents = $(window).data("events");
     if (windowEvents.unload.length > 0) {
          // some fun stuff
     }

Of course, in jQuery 1.8 this ability was removed. After some research, I found a suggestion for getting around this:

     var windowEvents = $._data($(window), "events");

But, according to the console, windowEvents is undefined. What am I doing wrong? Any other suggestions?

Thanks in advance :)

like image 544
ahammond Avatar asked Jul 09 '13 17:07

ahammond


1 Answers

Seems to work for me with $._data if the event handler is bound :

$(window).on('unload', function() {
   // do stuff
});

if (($._data( window, 'events' ).unload || []).length) {
    console.log('unload is bound');
}else{
    console.log('unload is NOT bound');
}

FIDDLE

like image 118
adeneo Avatar answered Sep 28 '22 23:09

adeneo