0

I want to check if scroll event was bound to window element in jQuery. Basically I want to see if someone has done something like this:

$(window).scroll(function() { ... });

After looking for a little bit on Stack Overflow, I have found more common question like if event already exist on an element with a working solution.

But when I tried to use $.data( $(window).get(0), 'events' ) I get undefined. Similar solution is proposed here as well.

So what is the correct way to check for scroll event?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

1 Answers1

2

I guess this is what you want:

$._data(window).events.scroll

That return undefined if there is no such event was binded to the window object

Example:

JSFiddle

alert($._data(window).events.scroll); // Should return 'undefined'

$(window).scroll(function() {
    alert('a');
});

alert($._data(window).events.scroll); // Should return 1 object

$(window).scroll(function() {
    alert('b');
});

alert($._data(window).events.scroll); // Should return 2 objects
Tony Dinh
  • 6,668
  • 5
  • 39
  • 58