I’m confused about where to place JavaScript functions:
When should they be put within the head
When inline within the body
And, when after the closing html tag?
thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The rules are fast and loose on this, There is no right or wrong way only better and less-better. (well after the
</html>is wrong)Generally speaking, javascript in the
headof the document might block rendering of the page until the file is loaded in some browsers *cough*IE*cough*. This is due to a limit of simultaneous connections. So some people put them before the closinghtmltag. You can use a library to asynchronously load the javascript to avoid this blocking.If you’re using a library, or checking for the DOM to be loaded before executing code there’s really no issue of where it’s placed. However if you’re not doing that it’s probably better to put it at the end.