Rectangle 27 0

javascript Is it wrong to place the script tag after the body tag?


<html>
  ....
  <body>
     ....
     <script type="text/javascript" src="theJs.js"></script>
  </body>
</html>

@PHPst: well, invalid code may be subject to side effects in certain browsers. Either way, I don't see how its indentation being one tab-width less than the code above it makes it look any cleaner.

@epalla: if you put the script right at the end of the body tag there's no other content left to load by the time it gets there, so there should be little difference between placing it outside or just inside. You then have the added benefit of your page still validating, which was the point I was trying to make in my answer.

It won't validate outside of the <body> or <head> tags. It also won't make much difference unless you're doing DOM manipulations that could break IE before the body element is fully loaded to putting it just before the closing </body>.

Note that apps like YSlow will actually suggest that you do include your javascript at the end of the page. It may not speed up overall load time but it may load the relevant content first. Still, putting it just inside the </body> tag is best.

Yep, I was agreeing with you since your answer is good. I just wanted to add that there is a reason for putting JS at the bottom of the page instead of in the head as we've done for a long time.

Note
Rectangle 27 0

javascript Is it wrong to place the script tag after the body tag?


<html>
  ....
  <body>
     ....
     <script type="text/javascript" src="theJs.js"></script>
  </body>
</html>

@PHPst: well, invalid code may be subject to side effects in certain browsers. Either way, I don't see how its indentation being one tab-width less than the code above it makes it look any cleaner.

@epalla: if you put the script right at the end of the body tag there's no other content left to load by the time it gets there, so there should be little difference between placing it outside or just inside. You then have the added benefit of your page still validating, which was the point I was trying to make in my answer.

It won't validate outside of the <body> or <head> tags. It also won't make much difference unless you're doing DOM manipulations that could break IE before the body element is fully loaded to putting it just before the closing </body>.

Note that apps like YSlow will actually suggest that you do include your javascript at the end of the page. It may not speed up overall load time but it may load the relevant content first. Still, putting it just inside the </body> tag is best.

Yep, I was agreeing with you since your answer is good. I just wanted to add that there is a reason for putting JS at the bottom of the page instead of in the head as we've done for a long time.

Note
Rectangle 27 0

javascript Is it wrong to place the script tag after the body tag?


And yet Google does this in their example of how to do G+ sign-in, with "last updated April 10, 2014". I got it from the version for Java on the server (developers.google.com/+/quickstart/java) but presumably it is the same HTML+js for all.

IE doesn't allow this anymore (since Version 10, I believe) and will ignore such scripts. FF and Chrome still tolerate them, but there are chances that some day they will drop this as non-standard.

Note
Rectangle 27 0

javascript Is it wrong to place the script tag after the body tag?


This is a better answer. There are too many new browsers out there with mobile coming into play to risk doing it wrong when all you have to is cut and paste a single closing tag.

Note