views:

128

answers:

4

I am using lazy load jquery plugin.

It tells me to put this in my code to activate it:

$("img").lazyload();

Do I have to put this in $(document).ready()? or can I just put it at the very top of the page:

<head>
<script type="text/javascript">
$("img").lazyload();

$(document).ready...{}

</script>
</head>
A: 

You need to put it inside since it uses jQuery library.

hallie
I'm fairly new to jQuery/js so, if I'm wrong, be nice? But once the jQuery library's loaded aren't the functions therein accessible, so long as they're in an appropriate form, available to all scripts in the calling page so long as they're within the appropriate `<script>` tags?
David Thomas
Just because it uses jquery, that doesn't mean it needs to go inside.
Mike Sherov
+3  A: 

The demo page puts it in the $(document).ready(), except it uses the shorthand $( ) function to do it.

(from the demo)

$( function() {
    $("img").lazyload({placeholder : "img/grey.gif"});
} );

(note that $() is an alias to the jQuery() function, which takes CSS selectors, HTML elements and also callbacks to run when the DOM loads)

If you don't put it in the ready() function, then it may only affect images that are earlier in the page than your script. And if you're placing scripts in the HEAD, that's none of the images.

So, unless the library is somehow using the .live() function of JQuery, you need to put it in ready().

kibibu
Accepted with multiple down-votes? *Why* the down-votes..?
David Thomas
Maybe I am wrong about how .live() works? Or about putting the script halfway down the page? Who knows!
kibibu
@kibibu, possibly, but my question wasn't to you, as such, but I was hoping that whoever down-voted might explain *why* they did so. Or, failing that, someone who knows more about js/jQuery than me, might explain.
David Thomas
I think it's a good answer.
TIMEX
Random anonymous down-votes is one of the most fun things about stackoverflow.
kibibu
@kibibu, it's one of the many reasons that I think actions should be *owned* by whoever takes those actions. It works well for *closing* questions, I don't see why it'd be a problem for down-voting an answer. I think it'd encourage more reasonable behaviour.
David Thomas
I just came back to this question to see what I downvoted. I changed it to +1 instead of -1. I must've fat fingered this accidentally. The buttons are hard to deal with on mobile phones. Sorry about this.
Mike Sherov
+3  A: 

The way I understand it is that if you put it in the $(document).ready(...) the script won't run until the DOM has loaded. If you just put it in <script></script> tags in the head then the page will have to wait for the script to complete before the remainder of the DOM can load.

If the script modifies the DOM this means it will run, do nothing, then the page will load (having not been affected by the $("img").lazyload() function.

David Thomas
I was also going to accept this, but I Can't accept two!
TIMEX
@alex, well, to be fair, I've already gained more rep than @kibibu for some reason. Despite the non-acceptance. =)
David Thomas
+2  A: 

You should put it in the $(document).ready().

Corey Sunwold