Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery code placement suggestion

Tags:

jquery

php

I know this is going to sound like a dumb question, but as I'm learning jQuery by example, I'm finding that the placement of the scripts and functions varies a lot from example to example. Case in point, somewhere I read that the .onReady function should be placed below everything else to insure that the whole DOM is actually ready, and things of that nature.

The question is simply this, aside from obvous script tags, is there a Best Practices of where in the PHP file jquery should located? What about one-off inline scripts? Sorry for the naive nature of this, but I'd to be trying these examples in a "right" way as I figure out how to bring it all together.

This site seems to have some very insightful folks contributing, so thanks in advance for any guidance! :)

Aparently it wasn't as dumb as I thought - thank you everyone for the insights - I feel a little more clarity on what I was trying to understand in the big picture.

like image 436
Al Knight Avatar asked May 24 '12 16:05

Al Knight


1 Answers

AFAIK, there is no .onReady

Perhaps you are referring to $(document).ready()?

The point of the .ready() is to wait for the element to be ready. In this case, the document. So nothing within that will be executed until the document is ready. As such, you can put that anywhere you want.

As for where you link to JS files, however, you want to do that at the bottom of the document for performance reasons:

http://developer.yahoo.com/performance/rules.html

like image 95
DA. Avatar answered Sep 28 '22 00:09

DA.