NYCPHP Meetup

NYPHP.org

[nycphp-talk] JavaScript just above form in body?

John Campbell jcampbell1 at gmail.com
Thu Feb 7 12:39:58 EST 2008


On Feb 7, 2008 12:00 PM, Rahmin Pavlovic <rahmin at insite-out.com> wrote:
>
> Michael B Allen wrote:
>
> > Is it considered bad practice to emit scripts within the body of a
> > page such as immediately preceeding the form that uses it? It seems to
> > work fine but from looking at some random sites I have yet to see an
> > example of this. Logically it makes sense to put the JavaScript close
> > to where it will be used no?
> >
>
> That actually makes perfect sense in terms of download speed.  It became
> somewhat of a standard practice to include JS in the HEAD of the document,
> but that actually slows things down.
>
> I actually wrote about this and a bunch of other JS optimization tricks in
> my well-ignored Wordpress blog (primarily ignored by me):
> http://insite-out.com/#load-scripts-last

Insightful post, although I disagree with the bit about destructors.
I prefer to let the scope+garbage collection destroy objects rather
than doing it manually.

The key thing to understand is that the browser stops rendering the
page when it encounters a <script> tag.  The script is fully parsed
and executed, before rendering resumes.  Browsers must work this way
because the script may contain "document.write".   Presently, the
defer attribute is not supported across all browsers thus it is not a
viable workaround.


-John C.



More information about the talk mailing list