Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading script outside of html document

I came accross this html snippet & to my surprise jQuery Object does get created!

<!doctype html>
<body>
<h1>Hello World</h1>
</body>
</html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>

As you may notice <script> tag comes AFTER closing </html> tag. I wonder how & why does this even work?

like image 343
CuriousMind Avatar asked Aug 01 '11 08:08

CuriousMind


3 Answers

It works because browsers are very forgiving, there is no benefit to doing it this way.

The reason it is there, is probably because someone misunderstood what was meant by 'putting scripts as far down the page as possible'…

like image 174
Matijs Avatar answered Sep 20 '22 17:09

Matijs


If you validate the file, it would say it is not valid. However, browsers will usualy try and do things the way you probably meant them.

Browsers just aren't that strict. You can do alot of things wrong and they would still work.

like image 44
Johan Avatar answered Sep 20 '22 17:09

Johan


I checked this code for firefox , script is treated as inside body tag for this particular case .

Browsers are really Error tolerant indeed .

like image 24
Sandeep Pathak Avatar answered Sep 20 '22 17:09

Sandeep Pathak