[wp-hackers] Including javascript in the footer
Mike Little
wordpress at zed1.com
Wed May 11 10:13:06 UTC 2011
On Wed, May 11, 2011 at 08:01, Philip Walton <philip at philipwalton.com>wrote:
> On 5/10/11 11:37 PM, Otto wrote:
>
>> If you're not deciding until midway through the page, then
>> you're-doing-it-wrong (tm). You can't enqueue once scripts have
>> printed in the header, due to possible dependencies. For example, if a
>> script enqueued in the head depends on a script enqueued in the
>> footer, then the footer script gets promoted to the header. This is
>> part of the purpose of the script dependency system.
>>
> I get the script dependency issue, but in my case I know for a fact that no
> script in the head will depend on a script in the footer.
>
> Also, if we're talking about the "right" way to do it, in my situation
> (granted you aren't seeing what I'm trying to do) I believe it's much more
> hackish and work-around-ish to move that decision to before the
> wp_enqueue_scripts hook.
>
> I would understand doing so if WordPress were unable to add this
> functionality, but to categorically say there is no situation where it could
> be the "right" way to go seems to go against the spirit of what we're all
> trying to do.
>
>
I see lots of conversations about not being able to do things in the header
because the decision is based on the content further down the page. But
often the thing overlooked is that the content has already been queried and
loaded from the db *before* the start of the page.
Therefore it is normally possible to peek ahead (at, for example,
$post->post_content) or even walk through the posts loop (don't forget to
rewind it after) to figure out if the javascript, extra css or whatever is
needed.
Mike
--
Mike Little
http://zed1.com/
More information about the wp-hackers
mailing list