JavaScript - JavaScript in Head and Body Tag

JavaScript can be written inside the both section and section. Sometime we need to write the JavaScript code in both section for better understand or calling the different functions. While writing JavaScript code and section we have to use

JavaScript can be written inside the both <head> section and <body> section. Sometime we need to write the JavaScript code in both section for better understand or calling the different functions. While writing JavaScript code <head> and <body> section we have to use <script> tag for both.

Here you can see an example where I have written JavaScript code in two ways by putting inside <head> section and by putting inside <body> section. These two are the formal ways to write scripting code.

I always put in the body section of HTML document because It will help to load the page faster.

HTML / JS Code


<html>
 <head>
        <script type="text/javascript">
   function book(){
    alert ("JavaScript is Awesome");
   }
     </script>
     </head>
    <body>
     <script type="text/javascript">
    document.write("Leave it!! No need to develop.");
  </script>
        <input type="button" onClick="book()" value="DEVELOP">
    </body>   
</html>

Output



        

You May Also Like...

Socialize with Us