December 20, 2015
External JavaScript Code
External 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.
HTML File
<html> <head> <title>JavaScript Tutorial </title> <script type="text/javascript" src="external.js"> </script> </head> <body> <input type="button" onclick="myFunction()" value="Click Me" /> </body> </html>
JavaScript File
function myFunction(){ alert("Hello World"); }