On Nov 5, 12:05 pm, "Sean Catchpole" <[EMAIL PROTECTED]> wrote:
> Is there a good reason why placing scripts at the bottom of a page is
> a bad idea?

wow I was about to come here and post the same thing.

I have a blog post about this:
http://michaelshadle.com/2007/10/30/handling-events-pre-documentready-in-jquery/

Basically I am trying to align with the Yahoo concepts too, but
placing jQuery at the bottom has some odd behaviors. Even with stuff
chained to $(document.ready(), I have events that are bound inside of
that but inside of the callbacks on the events I can raise a "$ is not
defined" and "jQuery is not defined" somehow. Somehow, the jQuery code
is being called, because it is executing the $(document) stuff, but it
still hasn't bound all the items in the DOM or assigned them to the
jQuery global object or the $ object or something.

Here's one example of that. You need Firefox. I am using Firefox
2.0.0.9 with Firebug and Yslow, just FYI.
http://mikehost.com/~mike/tmp/jquery/jquery1.html

If you click on one of the links, while it's loading that page if your
mouse moves over the other one it will raise the Javascript warning...

Also any <a href="javascript:foo()"> links that call jQuery related
stuff can't be clicked before jQuery is fully loaded. Having the
script towards the bottom can also mess that up. In my blog post I
wound up using an ID and a callback so it can only work if jquery is
loaded, however, it winds up acting like a dead link until everything
is loaded fully (extremely poor user experience) and actually stays
dead a lot of the time (for reasons unknown to me) - I've also tried $
(window).load() and some other stuff. For the mouseovers, I might be
able to do some if ($ && typeof $.isReady == 'boolean' && $.isReady)
or something to check for the existence of $ ... but the question
still remains for more things.

Maybe John monitors this list or another hardcore Javascript guy and
could chime in....

Can jQuery be put at the bottom of the page safely, or is using jQuery
with Yahoo JS optimization tips mutually exclusive?

And if so, I guess the fix is to include jquery in the <head> (as soon
as possible) and the rest can still be in the footer...

Reply via email to