14

I have this funny bug that occurs and I am at a loss as to how to debug it. Every time a page loads on my site a scroll event fires. The page doesn't visibly move, and i certainly am not triggering the scrolling via the mouse or keyboard. I know that scroll event is firing because i put a line of code that reads

$(window).bind('scroll', function (e) {console.log(e)});

Sure enough on every page i get a little "jQuery.Event" message in my console's log. When i break point it my call stack ends at jQuery.even.dispatch.apply(eventHandle.elem, arguments), which doesn't give me a ton to work with.

Here's the question. How do I find out what is triggering this scroll event? Is there an attribute in the jquery event object that will tell me if the scroll was user triggered or triggered by a script? In this situation what would you do to figure this out?

aamiri
  • 2,420
  • 4
  • 38
  • 58
  • You could try setting breakpoints in your browser and try to narrow down the source manually. – adamdport Sep 13 '12 at 18:08
  • read the line, "When i break point it my call stack ends at jQuery.even.dispatch.apply(eventHandle.elem, arguments), which doesn't give me a ton to work with." – aamiri Sep 13 '12 at 18:24
  • Could this possibly be a stack overflow issue? I've seen event handling in jQuery do this before... how long is the call stack? – Steven Hunt Sep 13 '12 at 18:27
  • there are three functions on the stack :-/ – aamiri Sep 13 '12 at 18:51
  • what do you have in `e.target` and `e.which` ?? – Jarry Sep 13 '12 at 19:27
  • target is htmldocument, and which is undefined – aamiri Sep 14 '12 at 14:15
  • I don't know if this could shed any light, but the project im working on where this issue reared it's head was a responsive redesign. Also the website adds a lot of content dynamically after page load via jquery. – aamiri Sep 14 '12 at 14:17
  • 2
    I have the exact same problem. I'm also at a loss of how to find what triggers the scroll event. My app is also a jquery-based responsive website. Did you find a way in the meantime? – ib84 Jun 07 '14 at 12:04
  • @aamiri did you find any solution to this? I'm having the same problem right now though in an Android Cordova app that isn't using jQuery. – Uche Ozoemena Jan 04 '21 at 08:47

1 Answers1

2

Alright, it looks like jQuery hides all event binding data within a hidden attribute. This post describes ways that lets you find out at least what is being run -- it is still your responsibility to find out where the handlers are in which file.

In the case where scroll events are involved:

var scrollHandlers = jQuery._data(window, 'events')['scroll'];
for (var i = 0; i < scrollHandlers.length; i++) {
    console.error(scrollHandlers[i].handler);  // or console.debug, whatever proves they exist
}
Community
  • 1
  • 1
Brian
  • 7,394
  • 3
  • 25
  • 46
  • Doesn't really answer the question because his scroll is probably being triggered in an onload or somesuch and this only finds scroll handlers. But +1 anyway because `jQuery._data(window, 'events')` was a great thing to learn. – dlsso Apr 07 '16 at 22:17