External JavaScript
Some Scripts can also be placed in external files. Which further used in different web pages according to the needs of the user or a webpage document.
External Scripts are useful and practical when the same code is used in a number of different web pages.
With the help of external javascript, we increase the property of code reusability because of single javascript file used in various web pages just by using the source address of the javascript code.
External JavaScript files must be saved by .js extension. Here, we use an src attribute in order to use the external javascript file.
Syntax:
<script type = “text/javascript” src = “demo.js”></script>
Code:
alert("Hello TutorialsLink!");
<html>
<head>
<script src="demo.js"></script>
</head>
<body>
<h1>External JavaScript</h1>
</body>
</html>
As the External JavaScript is described by using this code. Here, the script file in written in a different file which is saved as “demo.js” and the HTML code is written in a different file which is saved as “index.html”.
Further, with the help of “src” attribute, we can easily use the functionality of the javascript file in an any HTML file.
Output:
We can place the external script reference in <head> or <body> section as well.
External JavaScript Advantages: