Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing Javascript in a HTML file [duplicate]

Possible Duplicate:
How bad is it to embed JavaScript into the body of HTML?

Ok, so I've been using JavaScript for a good while now and I'm no stranger to it, but my question is in regards to where the JavaScript should actually be placed in a HTML file.

I've always been told and worked under the impression that all of your references to external script files should be contained in the head tag and any script that is contained in the current HTML file should be at the end of the file.

Now, more recently and having discussions with co-workers I have seen more and more people placing script blocks in seemingly random places throughout the HTML page, such as

<div>
   <p>Foo</p>
   <script type="text/javascript">
       //some script
   </script>
   <p>Bar</p>
</div>

Now is this good practice? I've always thought that it wasn't. If so, is it beneficial because the script gets executed at that point, but then why can't you wait until the rest of the HTML has been loaded?

like image 496
mattytommo Avatar asked Dec 01 '22 06:12

mattytommo


1 Answers

+1 vote for at the bottom of the page

give visitors the;

  • content and markup first

  • then display with css

  • then ui with javascript

appreciate this is a tad high brow

like image 181
Rob Sedgwick Avatar answered Dec 04 '22 11:12

Rob Sedgwick