Should I always put my JavaScript file in the head tag of my HTML file?

JavascriptWeb DevelopmentFront End Technology

You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tag or <body> tag.

It’s good for performance to add JavaScript in <body> element. Here’s an example to add <script> under <body>…</body> −

<html>
   <body>
      <script>
         <!--
            document.write("Hello World!")
         //-->
      </script>
   </body>
</html>


Here’s an example to add <script> under <head>…</head> −

<html>
   <head>
      <script>
         <!--
            document.write("Hello World!")
         //-->
       </script>
   </head>
   <body>

   </body>
</html>
raja
Ali
Published on 10-Jan-2018 16:06:44
Advertisements