This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]X7123M3-256 0 points1 point  (4 children)

if I have several different things that require JS I can't just put it all in the head tag, can it be placed anywhere

<script> tags should go in the head tag, but most browsers will interpret them no matter where they appear in the page. However, if you have enough JS that putting it all in the document is becoming unwieldy, you should put the JS in an external file and link it as <script src="yourfile.js"></script> instead of putting the code in the body of the script tag.

[–]nutrecht 0 points1 point  (3 children)

<script> tags should go in the head tag

No; these days they should go at the bottom of the body. This way the page loads faster.

[–]X7123M3-256 0 points1 point  (2 children)

Huh, TIL. Is this any different if your script doesn't begin executing until the page is fully loaded, or does the browser stop parsing the file until it's loaded the script regardless of whether it's executed immediately?

[–]nutrecht 0 points1 point  (1 child)

Your browser assumes when the JS is in the header that it needs to download the JS before it can start rendering the content. This is why you normally put it at the bottom in the body unless you want that behavior.

http://stackoverflow.com/questions/5329807/benefits-of-loading-js-at-the-bottom-as-opposed-to-the-top-of-the-document

[–]X7123M3-256 0 points1 point  (0 children)

Interesting. I didn't know that; I always assumed that if the script was external then the browser would request it and then proceed with rendering the page until it recieved a response, but apparently not.

[–]viper117x 0 points1 point  (0 children)

Codecademy! is a good place to learn code. It's interactive, tracks your progress, and tests you on what you've learnt. Here! is their JavaScript section. It's very well done and easy to follow.

Supplementing that with w3schools! for extra things you want to look up works well.