Why i called the script tag inside the head tag,why not after the head tag? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why i called the script tag inside the head tag,why not after the head tag?

13th May 2017, 8:35 AM
Safial Muntasir
Safial Muntasir - avatar
4 Answers
+ 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.
13th May 2017, 9:03 AM
Apel Mahmod
Apel Mahmod - avatar
+ 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 )
13th May 2017, 8:43 AM
visph
visph - avatar
+ 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.
13th May 2017, 8:42 AM
Calviղ
Calviղ - avatar
+ 2
Thank u both visph & calvin
13th May 2017, 8:51 AM
Safial Muntasir
Safial Muntasir - avatar