admin管理员组

文章数量:1313105

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 :)

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 :)

Share Improve this question asked Jul 9, 2013 at 17:26 ahammondahammond 1252 silver badges7 bronze badges 4
  • Does this work $(window).data('__events__');? – tymeJV Commented Jul 9, 2013 at 17:29
  • 2 I wouldn't write code that relies on undocumented features, like event handlers being stored in a particular location. – user2437417 Commented Jul 9, 2013 at 17:33
  • Have you bound any handlers to the window ? – Gabriele Petrioli Commented Jul 9, 2013 at 17:35
  • Can you describe why you're using that check? What is the fun stuff? – Dave Methvin Commented Jul 9, 2013 at 17:50
Add a ment  | 

4 Answers 4

Reset to default 3

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

I found solution on http://dbj/dbj/2013/04/09/list-of-jquery-events/

it's base is:

/* (c) 2013 by DBJ.ORG, GPL/MIT applies */
jQuery.events = function (expr ) {
  var rez = [], evo ;
   jQuery(expr).each(
      function () {
         if ( evo = jQuery._data( this, "events"))
           rez.push({ element: this, events: evo }) ;
     });
 return rez.length > 0 ? rez : null ;
}

To use it just call:

jQuery.events("div");

Where div is place for any jquery selector. I've tried it on jQuery 1.9 and it work very well for me.

I ended up including a check to see if windowEvents was defined. In addition I included the [0] when asking for the events (this solution was found here: jQuery 1.8 find event handlers). So the final code looks like this:

var windowEvents = $._data($(window)[0], "events");
if (windowEvents && windowEvents.unload.length > 0) {
    // some fun stuff
}

This is now functioning as it did before the upgrade. Thanks for your help!

They are still stored in the same place. But it stores only events that have been bound, and only after at least one is bound (otherwise data() will return undefined) .

So you will have to change your condition

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

本文标签: javascriptHow can I get all window events in JQuery 19Stack Overflow