Site icon Eval

JavaScript Tutorial & Demo 4: External JavaScript

JavaScript Tutorial & Demo 4 External JavaScript

Using External JavaScript: The script tag allows you to save JavaScript in an external file and subsequently include it in your HTML files. We can generate an external JavaScript file and embed it in various HTML pages. It allows for code reuse because a single JavaScript file may be used in several HTML pages.

External Java Script Demo Example

<html>
   <head>
      <script type = "text/javascript" src = "extscr.js" ></script>
   </head>
   <body>
           ........
   </body>
</html>

The .js extension must be used to save an external JavaScript file. It is suggested that all JavaScript scripts be embedded into a single file. It improves the page’s performance.
Include a JavaScript file from another source.

Steps to include a JavaScript file

To incorporate JavaScript from an external file, do the following:

Creating a file with the extension.js, such as exscr.js, and storing it in the js subdirectory. It should be noted that while storing the JavaScript file under the js folder is not mandatory, but it is a good practice.

Then, in the <script> element’s src property, utilize the JavasScript source code file URL.

It is important to note that you can load a JavaScript file from a remote site. This enables you to serve JavaScript from several domains, such as a content delivery network (CDN), to speed up the website.

Using Multiple JavaScript files

When there are multiple JavaScript files on a page, the JavaScript engine evaluates them in the order they appear. Consider the following:

<script src="js/exscr.js"></script>
<script src="js/service.js"></script>

The exscr.js and service.js files will be interpreted sequentially by the JavaScript engine. Before understanding the service.js file, it finishes interpreting the exscr.js file.

During the page rendering phase, the blank page is displayed for the page that contains multiple external JavaScript files. To overcome this, include the JavaScript file exactly before the </body> element, as illustrated in the following example:

Demo Example: External JavaScript

<!DOCTYPE html>
<html >
<head>
   <title>JavaScript External JavaScript Example</title>
</head>
<body>
 
   <!-- end of page content here-->
   <script src="js/exscr.js"></script>
   <script src="js/service.js"></script>
</body>
</html>

The async and defer properties

To modify how JavaScript code loads and executes, utilize one of the <script> element’s properties: async or defer.

Demo Example: async

<script async src="exscr.js"></script>
<script async src="service.js"></script>

Because the service.js file may run before the exscr.js file, you must guarantee no dependence between them.

Demo Example: defer

If the defer attribute is present, it instructs the web browser to run the script file after processing the content.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>JavaScript defer demo</title>
    <script defer src="defer-script.js"></script>
</head>
<body>

        ...................                  
</body>
</html>

Even though we included the <script> element in the <head> section, the script will not run until the browser receives the ending tag <html>.

External JavaScript Benefits

External JavaScript Drawbacks

Article Category: JavaScript Tutorials

More JavaScript Tutorials

Exit mobile version