Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Placement

JS Placement

We can place JavaScript code in many places within HTML or link external files that is internal JavaScript and External JavaScript

Internal JavaScript

Internal JavaScript included within the same HTML file.

Internal JavaScript included using <script> and </script> tags.

Internal JavaScript can be placed inside <head> element or <body> element.

JavaScript Inside <head>

JavaScript can be placed inside <head> element.

					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script>
                var clickTime = 0;
                function myFunction() {
                  document.getElementById("demo").innerHTML = "Button clicked " + (clickTime++) + " times.";
                }
              </script>
            </head>
            <body>
              <h1>JS Demo</h1>
              <p id="demo">Button clicked 0 times.</p>
              <button type="button" onclick="myFunction()">Try it</button>
            </body>
          </html>
        
      
Old JavaScript examples may use a type attribute: <script type="text/javascript">. The type attribute is not required. JavaScript is the default scripting language in HTML.
A JavaScript function is a block of JavaScript code, that can be executed when "called" for.
For example, a function can be called when an event occurs, like when the user clicks a button.
JavaScript Inside <body>

JavaScript can be placed inside <body> element.

					 
        
          <!DOCTYPE html>
          <html>
            <body>
              <h1>JS Demo</h1>
              <p id="demo">Button clicked 0 times.</p>
              <button type="button" onclick="myFunction()">Try it</button>

              <script>
                var clickTime = 0;
                function myFunction() {
                  document.getElementById("demo").innerHTML = "Button clicked " + (clickTime++) + " times.";
                }
              </script>
            </body>
          </html>
        
      
Placing scripts at the bottom of the <body> element improves the display speed, because script interpretation slows down the display.
External JavaScript

External JavaScript included using <script> tag pointing to a JavaScript file or resource using src attribute.

JavaScript files must have file extension .js

External JavaScript can be placed inside <head> element or <body> element as well.

External JavaScript File: script.js
					 
        
          var clickTime = 0;
          function myFunction() {
            document.getElementById("demo").innerHTML = "Button clicked " + (clickTime++) + " times.";
          }
        
      
External scripts cannot contain <script> tags.
JavaScript Inside <head>
					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="script.js"></script>
            </head>
            <body>
              <h1>JS Demo</h1>
              <p id="demo">Button clicked 0 times.</p>
              <button type="button" onclick="myFunction()">Try it</button>
            </body>
          </html>
        
      
JavaScript Inside <body>
					 
        
          <!DOCTYPE html>
          <html>
            <body>
              <h1>JS Demo</h1>
              <p id="demo">Button clicked 0 times.</p>
              <button type="button" onclick="myFunction()">Try it</button>

              <script src="script.js"></script>
            </body>
          </html>
        
      
External JavaScript Advantages

It separates HTML and code

It makes HTML and JavaScript easier to read and maintain

Cached JavaScript files can speed up page loads

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved