0
Why i called the script tag inside the head tag,why not after the head tag?
4 Respuestas
+ 5
Generally developers add script tag at the end of the body section. this ensure that HTML DOM is loaded before you can interact with DOM.
In SL code playground you have to add script tag inside head section ( only if you write JS in playground JS Tab). For example If you add jquery at the end of the body tag and use jquery in JS tab then you will get error "$ or jQuery is not defined".
REMEMBER this is only applicable if you use playground and write Javascript inside JS tab.
You can avoid this if you write JS code inside HTML script tag. For example..
<html>
<head>
</head>
<body>
<script src="jquery.js"></script>
<script>
// write application code here
// jQuery already loaded
</script>
</body>
</html>
Outside SL code playground or real project always try to add script ad the end of the body.
+ 7
Why not: you can as well put it ( them ) anywhere inside the <body> content ^^
It's depends on your needs ;) ( there are some technical differences to put somewhere or elsewhere :P )
+ 2
You should call your script after all the html codes and before closing html body tag, not in the head tag.
Reason being is there is the place where the DOM have loaded all HTML elements and ready for Javascript to access it.
+ 2
Thank u both visph & calvin