views:

43

answers:

4

I used to put my jquery and javascript stuff just BEFORE the /head... on top of the html document....

Thinking about that, for NO good reason...

Now, in some part of the document, there will be generated part in php and i can used some jquery here and there..

So the question is : can i put jqueyr (document).ready on top and anywhere i like to be.... in the body...or there is some restriction or speed/parsing problem that will rise

Any though on that will light up my world !

A: 

Try it! and see what happens.

You can put the (document).ready inside the body tag

Cesar
A: 

yes you can. it really makes no difference as document.ready() is simply setting up an event handler for code that executes AFTER the DOM has arrived in the browser.

Scott Evernden
+1  A: 

Technically, it should be fine. Multiple $(document).ready are perfectly legitimate and will work as expected, and even execute in order. The two things to consider are:

  • Readability - could seriously clutter up the output with code, which is somewhat defiant of the purpose of jQuery.
  • Scope - a function declared in one $().ready block is cannot be seen from the other $().ready(... blocks.

Some discussion can be found (a bit dated) but here it is anyway:

http://www.learningjquery.com/2006/09/multiple-document-ready

karim79
A: 

I am not sure on this... but from what I know, stuff in section have a greater priority on the loading of the html

have you ever seen a website that has lost it's CSS? if you check for the source-code, their CSS is probably outside the head section, because the browser didn't try too many times the connection to it

but I can be wrong about this.

Jonathan