[wp-hackers] Including javascript in the footer

Otto otto at ottodestruct.com
Wed May 11 14:03:19 UTC 2011


On Wed, May 11, 2011 at 1:42 AM, Dion Hulse (dd32) <wordpress at dd32.id.au> wrote:
> It is an "impossibility" for a header script to rely upon a footer script
> enqueued mid-way through the page load.

No, it isn't. It's actually rather easy to do. And if done in the most
obvious way, then it breaks the page.

I agree with you that with the existing code, it's an impossibility to
enqueue anything after wp_print_scripts has run for the first time,
period. The difference is that I think that that is correct and should
be the way it works by design.

> It is possible that a header script may modify how it runs if another script
> is loaded, and loading said script in the footer would break the expected
> bejaviour, however in that case, it would be wrong to load such a script
> mid-way through the page.

IMO, it's wrong to load any script midway through the page at all.
Halfway is just too late to be still trying to load scripts. Make your
decision earlier. Why are you still deciding on how to display the
page while you're halfway through displaying it? Seems silly to go to
all this trouble when you can easily look-ahead at the content from
the header.

There does not have to be only one Loop through the main posts on the
page. I don't know where people get this assumption from, but if you
assume that there is one-and-only-one main Loop, then you will
invariably write code that breaks, badly, when this is not the case.
I've seen several plugins break because of this particular incorrect
assumption.

-Otto


More information about the wp-hackers mailing list