Re: why add script-tag in header section ?



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.

.



Relevant Pages

  • Re: Sound of j and z in Hindi films and songs
    ... "Official Policy" to remove the dots from j-dot etc, ... there was a practice of writing an anuswar on certain words like ... the dictionaries and the script followed later. ... I also notice that the more vibrant a language more tolerant it is. ...
    (rec.music.indian.misc)
  • Re: Can com+ Hotfix Rollup Package 27 fix my memory problem with WSH. CreateObject/CreateScript?
    ... Heh, improve your aim, then... ... Of course in script, the object will be ... > ignore best practice at your own risk. ...
    (microsoft.public.scripting.wsh)
  • Re: Javascript Best Practices Document v1.0
    ... that document.formsis a bad practice when it is a DOM 2 HTML valid practice in XHTML ... client-side form validation), then use an id attribute, or pass references directly and omit an identifier entirely. ... There is no general rule for returning false in a script, ...
    (comp.lang.javascript)
  • Re: Active Directory password expiry notification via email??
    ... I don't think that there is a way to do this natively but you could write a script that compares todays date with the Users PasswordExpirationDate property. ... New York, New York 10024-5192 ... Hacking, like any art, will take years of dedicated study and practice to master. ... SensePost willl be at Black Hat Vegas in July. ...
    (Security-Basics)
  • Re: External js files, document.write, and (apparent) deferred evaluation
    ... Well, it's definitely one way to achieve modularity, but apparently not ... javascript interpreter. ... Since you "include" the script without condition, ... and the type attribute is required for Valid HTML 4/XHTML 1?) ...
    (comp.lang.javascript)