3

Have read that it is possible to detect a scroll with this line:

$('window').one('scroll', function() {  } );

If have this inline code:

var TimeVariable=setInterval(function(){ DoJQueryStuff() },300);

function DoJQueryStuff()
 {
 if(typeof jQuery == "undefined") return;
 window.clearInterval(TimeVariable);
 $('body').one('mousemove', function() { alert('mouse'); } );
 $('window').one('scroll', function() { alert('scroll'); } );
 }

DoJQueryStuff is called every 300 ms until JQuery is loaded. If I move the mouse anywhere on the screen, I got the "mouse" alert. If I scroll down I do NOT get the "scroll" alert. i.e. it's not firing.

Any help would be greatly appreciated.

David

2
  • 1
    Rather than checking every 300ms until jQuery loads, would it be possible to add the function into the ready function? $(document).ready(function() { do stuff }); Commented Dec 3, 2013 at 22:12
  • Kyle, $(document) without Jquery loaded would cause an error. jquery is loading after the pages loaded (this is now what google suggests we do). An example of your idea would be appreciated Commented Dec 4, 2013 at 21:51

1 Answer 1

7

first please use jquery ready http://api.jquery.com/ready/ method. i dont want to say your implementation is wrong but is slower than the jquery ready method.

your scroll function don't get executed basically because you are binding it to the wrong object. when you refer to the window object with jquery don't wrap it in apostrophs.

if you do so, jquery will intern call the document.getElementsByTagName and can't find the tagname window because window is not an child of the window.document node. so your scroll detect function never gets fired because jquery can't bind a eventListener to your submited element tagname.

simply bind it to window without apostrophs. this forces jquery to bind the scroll event directly to the window DOM object where your function is correctly fired on scroll.

Example:

$(window).ready(function(){
    $(this).one('mousemove', function() { 
        // mouse move
    }).one('scroll', function(){
        // scroll 
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Google tells us to load jquery after the page has loaded. That's what I'm doing. Then I'm setting a timer to check that jquery has indeed been loaded. I am detecting the mouse move or scroll to load an external js file. Why does my scroll detect have to be in the ready function?
i updated my explanation - hope it becomes now more comprehensible
You can use an onload function when you load jQuery to run any code that depends on it. If you are using a library, it should have a way to add an onload function. If you are not, you can use <script onload="...">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.