External JavaScript Code

JavaScriptExternal JavaScript code is placed inside an external JavaScript file.  This external JavaScript file contains a lengthy JavaScript code.  If you place same lengthy code inside a HTML file, it will make your whole code file look ugly.  It’s a good habit to place all lengthy code inside an external JavaScript file and add path of it in the HTML file using src attribute of the script tag.  Sample code is given below.  In this scenario, both HTML and JavaScript files are located in the same folder.


<title>JavaScript Tutorial </title>

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


<input type="button" onclick="myFunction()" value="Click Me" />

JavaScript File
function myFunction(){
alert("Hello World");