Re: why add script-tag in header section ?
- From: "Richard Cornford" <Richard@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 16 Nov 2009 23:23:37 -0000
Christian Cambier wrote:
Hi.
"Though it is tempting to add JavaScript and CSS sources to
where you need them, proven best practice is to ensure they
are added to the header of a page"
why?
It is best practice to write valid HTML mark-up. Structurally valid (element nesting conforming with the applicable DTD) for purely technical reason; it encourages the browsers to produce consistent and predictable DOM strictures from the mark up (which are easier to script than variable, error-corrected and so unpredictable DOM structures). Validity with regard to attributes (specifically non-standard attributes) has never been shown to have a significant negative impact on scripting, but fully valid HTML has the advantage of being easily (and unambiguously) verified (A validator reporting many attribute related errors will make it harder to identify any structural error reports in its output, while a report of 100% validity is likely to be a single line of text).
The HTML DTDs require that STYLE elements are only ever direct children of the HEAD element. Thus, there is no alternative to placing the STYLE elements as children of the (possibly implied) HEAD element if structurally valid HTML mark-up is to be employed.
There are far fewer restrictions on the placement of SCRIPT elements in HTML documents, and there is no reason for imposing additional arbitrary restrictions on their placement. However, there are consequences to SCRIPT element placement due to script global variable instantiation/execution order.
Whenever anyone proposes a best practice, it is a good idea to require that they state a justification for that practice, in such a form that it is understandable, reasoned and convincing. Otherwise, what you are reading is probably just a personal opinion, in a world where the majority of commentators on browser scripting are more or less ill-informed/uninformed and/or suffering from misconceptions.
Richard.
.
- Follow-Ups:
- Re: why add script-tag in header section ?
- From: Hans-Georg Michna
- Re: why add script-tag in header section ?
- References:
- why add script-tag in header section ?
- From: Christian Cambier
- why add script-tag in header section ?
- Prev by Date: Re: FAQ Topic - I have window.status="Moomin"; why doesn't the statusbar change? (2009-11-04)
- Next by Date: Re: why add script-tag in header section ?
- Previous by thread: Re: why add script-tag in header section ?
- Next by thread: Re: why add script-tag in header section ?
- Index(es):
Relevant Pages
|