-2

At the 5 minute mark of the Advanced Techniques in JavaScript and jQuery class on PluralSight, Traditional JavaScript Functions segment, Kevin Murray puts his script tag after the closing body tag.

Is it now ok to put the scripts after the closing body tag?

Phillip Senn
  • 46,771
  • 90
  • 257
  • 373
  • 2
    As far as I know head and body are the only valid children of the html tag, so it's at least invalid markup. – adeneo Nov 22 '13 at 22:41
  • 5
    http://stackoverflow.com/questions/3037725/is-it-wrong-to-place-the-script-tag-after-the-body-tag – marteljn Nov 22 '13 at 22:41
  • 1
    ^ That. AndyE's answer is right on point too. – Dagg Nabbit Nov 22 '13 at 22:42
  • I guess it depends on the definition of okay. It certainly is functional. Some people use this as a way of (a) avoiding the use of `window.addEventListener('load', nameOfYourOnLoadFunctionHere, false);`, while others do it (b) as a way of ensuring the markup is loaded before the script. I wont offer more of an opinion than I have already implied. – enhzflep Nov 22 '13 at 22:43

2 Answers2

1

You should put them before the closing of section. That way your html code will load first and after that browser will process then your javascript code. So if there is any error or problem with your script the loading of your page will not freeze. In other words your html code will load and if there is any problem your js code will not work. On the other hand, If you had include your js code in the head section and any problem had arised, then your html code will not even load.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • 1
    I think you mean before the closing of the `` section. Sorry not trying to be picky but there is a `
    ` element now too. Just wanted to be clear and avoid confusion.
    – War10ck Nov 22 '13 at 22:44
  • 1
    @War10ck, no problem at all. Yeah, I mean before the closing of . At the bottom of the page, where there is no other html code. – Christos Nov 22 '13 at 22:45
1

Putting the script tag after the body tag is not correct according to the HTML standard, and there is hardly any reason to do so. A script tag should preferrably go inside the head element, or inside the body element if you have some specific reason for doing so.

You can put the script tag last in the body element instead of after it, with little or no difference on the functionality. All the elements in the document already exist at that point, so there is no normal reason to put it outside the body.

Putting the script tag outside the body will most likely work in all browsers, because they generally try hard to consider whatever HTML-ish mess you throw at them, but it doesn't follow the specification so there is no guarantee that all current browsers actually tolerate it, or that any future browsers do.

Also, there is a risk that invalid code like that will change how the browser interprets the entire HTML document. If the browser decides to parse the document in the backwards compatible quirks mode, it can have radical effects on the layout of the page.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005