[wp-hackers] Including javascript in the footer
Dion Hulse (dd32)
wordpress at dd32.id.au
Wed May 11 06:42:56 UTC 2011
Except thats a near-impossible scenario.
If a header script depends on a footer script, it'll be promoted to the
header (as expected), If it's not enqueued, It'll be enqueued.
It is an "impossibility" for a header script to rely upon a footer script
enqueued mid-way through the page load.
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.
The only type of script which could possibly benefit from this, are those
enqueued from shortcodes and plugin pages (although, there are better hooks
for plugin pages), if it has to be a limitation that the scripts enqueued
mid-load -must- be able to run in the footer, then there's no real problem..
and it prevents the need for looking ahead. If it requires a header script
thats not yet printed, that'll get printed in the footer as well..
On 11 May 2011 16:37, Otto <otto at ottodestruct.com> wrote:
> Enqueueing scripts and styles should always be done on either the
> wp_enqueue_scripts hook or the admin_enqueue_scripts hook, depending
> on which side of things you want to enqueue them on.
>
> This applies to both header and footer scripts. wp_print_scripts is
> really too late to enqueue, you should only dequeue there.
>
> 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.
>
> Redo your methodology to make the decision earlier. If this means you
> need to look ahead at the content, then do so.
>
> -Otto
>
>
>
> On Wed, May 11, 2011 at 1:32 AM, Philip Walton <philip at philipwalton.com>
> wrote:
> > I've tested wp_enqueue_script() and it doesn't work if called from any
> > action after 'wp_print_scripts' in a page request.
> >
> > The Codex recommends: "Use the wp_enqueue_scripts action to call this
> > function, or admin_enqueue_scripts to call it on the admin side. Calling
> it
> > outside of an action can lead to troubles."
> >
> > In my case I'm making a programmatic decision partway through the page
> > request whether or not I want to include a script in the footer, and
> > unfortunately, that decision is being made after the 'wp_print_scripts'
> hook
> > is fired.
> >
> > So what should I do? I don't want to just echo the script markup in
> > 'admin_print_footer_scripts' because that just seems wrong. However, I
> can't
> > really think of any other good way.
> >
> > Any ideas?
> >
> >
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list