3

Where is it legal to put a <script> tag in html?

I have a legitimate (I swear!) reason to use document.write in parts of my page. This means I need to put

<script>document.write('some goodness')</script>

at some arbitrary point in the DOM. Is that always supported?

chowey
  • 9,138
  • 6
  • 54
  • 84
  • 2
    There is no legitimate non-development use for `document.write`. – Niet the Dark Absol Nov 14 '13 at 18:29
  • Just before the closing body tag, according to this related post: http://stackoverflow.com/questions/436411/where-is-the-best-place-to-put-script-tags-in-html-markup?rq=1 – kunalbhat Nov 14 '13 at 18:30
  • 2
    Guys, I'm asking where is is *allowed*, not where you'd think it best to put it. – chowey Nov 14 '13 at 18:31
  • possible duplicate: http://stackoverflow.com/questions/436411/where-is-the-best-place-to-put-script-tags-in-html-markup?rq=1 – Naftali Nov 14 '13 at 18:31
  • @Neal, no that's not a duplicate. This one is explicitly about `document.write`, which makes a *huge* difference in script location. – zzzzBov Nov 14 '13 at 18:32
  • Not a duplicate since this asks about legal places, not optimal place. – Jukka K. Korpela Nov 14 '13 at 18:35
  • 1
    @NiettheDarkAbsol, [I can think of at least one legitimate use of `document.write`](http://programmers.stackexchange.com/questions/139372/referencing-external-javascript-vs-hosting-my-own-copy/139380#139380). – zzzzBov Nov 14 '13 at 18:37
  • The answer depends on HTML version. A complete answer would for this and other reasons (like the rather messy syntax of HTML5 in this respect) be rather large. It is questionable whether it would be useful. You should probably ask whether some particular place is valid or what the best place is, in a particular situation that you should describe. – Jukka K. Korpela Nov 14 '13 at 18:38
  • @JukkaK.Korpela, is that conjecture or is that based on hard evidence? I find that usually early, early features that stabilized back in the Netscape Navigator days tend to be left exactly the same, for fear of breaking stuff, unless they get officially deprecated. – chowey Nov 14 '13 at 18:41
  • @chowey, what are you asking about? I stated that this depends on HTML version. Do you seriously doubt that? – Jukka K. Korpela Nov 14 '13 at 18:54
  • Okay, so you are conjecturing because it is pretty obvious that things change on HTML version. I'll agree with that. It's just not a useful comment. – chowey Nov 14 '13 at 18:56
  • @chowey, it is not a conjecture, it is a basic fact of the situation and directly affects how the question should be answered, if the question were taken as asked. From your comments, it seems that you are actually interested in browser behavior, which is something completely different. This is yet another reason to close the question. Try asking about a real problem you are facing. – Jukka K. Korpela Nov 14 '13 at 19:03
  • @zzzzBov Would be "better" as `document.createElement('script')`... – Niet the Dark Absol Nov 14 '13 at 20:16
  • @NiettheDarkAbsol, how so? What would making it more verbose do for the script other than add additional unnecessary bytes and increasing the risk of adding the script to the wrong place in the document? – zzzzBov Nov 14 '13 at 20:22

2 Answers2

4

In HTML5 the <script> element is defined as being allowed in the following places:

Contexts in which this element can be used:

  • Where metadata content is expected.
  • Where phrasing content is expected.
  • Where script-supporting elements are expected.

Most content elements allow for phrasing content, and the <head> element allows for metadata content.

There are some elements that do not support the <script> element as direct descendants , and it should be noted that this list is subject to change, so consult the spec, as this is probably not a comprehensive list:

  • <html>
  • <title>
  • <script>

Additionally, you shouldn't put a <script> element in a <noscript> element because it simply doesn't make any sense to do so, even though it would technically be supported by the specification.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • I love this answer. But in my experience, putting a ` – chowey Nov 14 '13 at 18:52
  • The answer cites a mutable working draft without saying it. It only applies to one work-in-progress version of HTML. And e.g. `` surely “supports” the ` – Jukka K. Korpela Nov 14 '13 at 18:59
  • @JukkaK.Korpela, I can never keep the HTML5 spec links straight, [is this a better reference](http://www.w3.org/TR/html51/single-page.html#the-script-element), or do you have a different HTML5 spec in mind? – zzzzBov Nov 14 '13 at 19:11
3

You can include them wherever you want between your <head> or <body> tags :

  • If you insert it in the head, it'll be loaded before the page rendering,
  • If you place it on the body, it will be loaded once all the page is rendered, so the user won't wait for your script to be parsed before the pages renders = faster page rendering.

Exceptions :

  • Inside a <title> tag (as you said)
  • Inside a <tr> (see this link) Apparently it does (see after)

I tried with a few elements on that fiddle, you can try it

Community
  • 1
  • 1
Maen
  • 10,603
  • 3
  • 45
  • 71