Docsity

External JavaScript Files: Advantages, Disadvantages, and Best Practices, Study notes of Javascript programming

Toggle sidebar
bg1
External JavaScript file
We can create external JavaScript file and embed it in many html page.
It provides๎˜›code re usability๎˜›because single JavaScript file can be used in several
html pages.
An external JavaScript file must be saved by .js extension. It is recommended to
embed all JavaScript files into a single file. It increases the speed of the webpage.
Let's create an external๎˜›JavaScript๎˜›file that prints Hello Javatpoint in a alert dialog
box.
message.js
1. function๎˜›msg(){๎˜›๎˜›
2. ๎˜›alert("Hello๎˜›Javatpoint");๎˜›๎˜›
3. }๎˜›๎˜›
Let's include the JavaScript file into๎˜›html๎˜›page. It calls the๎˜›JavaScript function๎˜›on
button click.
index.html
1. <html>๎˜›๎˜›
2. <head>๎˜›๎˜›
3. <script๎˜›type="text/javascript"๎˜›src="message.js"></script>๎˜›๎˜›
4. </head>๎˜›๎˜›
5. <body>๎˜›๎˜›
6. <p>Welcome๎˜›to๎˜›JavaScript</p>๎˜›๎˜›
7. <form>๎˜›๎˜›
8. <input๎˜›type="button"๎˜›value="click"๎˜›onclick="msg()"/>๎˜›๎˜›
9. </form>๎˜›๎˜›
10.</body>๎˜›๎˜›
11.</html>๎˜›๎˜›
Advantages of External JavaScript
There will be following benefits if a user creates an external javascript:
1. It helps in the reusability of code in more than one HTML file.
2. It allows easy code readability.
pf2

Partial preview of the text

Download External JavaScript Files: Advantages, Disadvantages, and Best Practices and more Study notes Javascript programming in PDF only on Docsity!

External JavaScript file

We can create external JavaScript file and embed it in many html page. It provides code re usability because single JavaScript file can be used in several html pages. An external JavaScript file must be saved by .js extension. It is recommended to embed all JavaScript files into a single file. It increases the speed of the webpage. Let's create an external JavaScript file that prints Hello Javatpoint in a alert dialog box. message.js

  1. function msg(){
  2. alert("Hello Javatpoint");
  3. } Let's include the JavaScript file into html page. It calls the JavaScript function on button click. index.html





  4. Welcome to JavaScript






Advantages of External JavaScript

There will be following benefits if a user creates an external javascript:

  1. It helps in the reusability of code in more than one HTML file.
  2. It allows easy code readability.
  1. It is time-efficient as web browsers cache the external js files, which further reduces the page loading time.
  2. It enables both web designers and coders to work with html and js files parallelly and separately, i.e., without facing any code conflictions.
  3. The length of the code reduces as only we need to specify the location of the js file.

Disadvantages of External JavaScript

There are the following disadvantages of external files:

  1. The stealer may download the coder's code using the url of the js file.
  2. If two js files are dependent on one another, then a failure in one file may affect the execution of the other dependent file.
  3. The web browser needs to make an additional http request to get the js code.
  4. A tiny to a large change in the js code may cause unexpected results in all its dependent files.
  5. We need to check each file that depends on the commonly created external javascript file.
  6. If it is a few lines of code, then better to implement the internal javascript code.