Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
179
rated 0 times [  184] [ 5]  / answers: 1 / hits: 17480  / 14 Years ago, tue, november 30, 2010, 12:00:00

I have had this question niggling at the curious bit of my mind for a while now and I thought I'd ask your collective expertise for an answer to this question.



To elaborate on the title, say I have this:




alert(Some JS outside );


Outside the <head></head> tags of my HTML file. My question is whether it's ok to do this or not, and how much it is used like this.



My instincts tell me it's ok - I reckon browsers look through all HTML for <script> tags and interpret it when they see it, so it should be ok, but I'm not all that great with how browsers work.



I'm looking for a definitive (or as close as possible to definitive) answer here - is it fine to do, or not?



EDIT: To save me posting this a bunch of times, I'll say it once here. Thanks very much for all your input people. Up votes to you all! I will have to re-train myself to put JS at the bottom of pages - now that I think about it it's blindingly obvious that scripts at the bottom of the page is way better than the top. Thanks for your help everyone.


More From » html

 Answers
221

Best place for your script tags is before your closing body tag.



<html>
<head>
<title>Example</title>
</head>
<body>
Your Content
<script type=text/javascript src=yourScriptHere.js></script>
<script type=text/javascript>//Inline scripts etc.</script>
</body>
</html>


That said they can be other places without a problem however the reason you want them at the end is that you want to ensure the page has loaded before execution and you also do not want to stall client download progress making them wait on large scripts.


[#94789] Saturday, November 27, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iyannae

Total Points: 147
Total Questions: 88
Total Answers: 120

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
;