External JavaScript

external in javascript

External JavaScript are useful and practical when the same code is used in a number of different web pages. JavaScript files have the file extension .js.

Scripts can also be placed in external files.

To use an external script, put the name of the script file in the src (source) attribute of the <script> tag.

message.js


function msg(){

 alert("Hello Learners");

}

index.html

<html>

<head>

<script type="text/javascript" src="message.js"></script>

</head>

<body>

<p>Welcome to JavaScript</p>

<form>

<input type="button" value="click" onclick="msg()"/>

</form>

</body>

</html>

Advantages of External JavaScript

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

Disadvantages of External JavaScript

There are the following disadvantages of external files and they are:

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

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *