Using an External JavaScript File

When you have prolonged JavaScript code, you can incorporate it an external file with .js extension. After that you need to link that external file with the HTML document you have created by “src” attribute of SCRIPT tag to access that file.

Demo

Create a document called ScriptInExternalFile.html



Syntax

The syntax for linking the external JavaScript file is as follows.

  1. <HEAD>  
  2. <SCRIPT src=”url of the external file” type=”text/javascript”>  
  3. </SCRIPT>  
  4. </HEAD>  
Note: In case your javascript file and the html document are in the same folder then you can just specify the name of the file followed by the extension (Relative URL) else you have to specify the complete path of the file (Absolute URL) as the value of SRC attribute of SCRIPT tag.

Write the following code to link the files.



The code for MyScript.js is as follows.



Execute the script by Opening the file in the Web Browser. If you are using Internet Explore click on “Allow Blocked Content” to allow the script to execute and you are using Mozilla Firefox then click on allow “ActiveX Controls”.



Thanks for reading my blog.

Ebook Download
View all
Learn
View all