Home » Online Computer Education » Lean Java Script, Referencing JavaScript in an External .js File, Lesson No. 17

Lean Java Script, Referencing JavaScript in an External .js File, Lesson No. 17

 

 

 

To store your JavaScripts in external files, you need to save them as plain text files with a .js file extension. You then need to add the SCR attribute to the opening <SCRIPT> tag in your HTML page as demonstrated here.

<SCRIPT SRC=”Test.js” LANGUAGE=”JavaScript”
TYPE=”Text/JavaScript”> </SCRIPT>
In this example, an external JavaScript named Test.js has been specified. This external JavaScript can contain any number of JavaScript statements. However, it cannot contain any HTML whatsoever. Otherwise you’ll end up with an error. For example, the contents of the Test.js script might be as simple as this:

document.write(“This is an external JavaScript.”);
There are many advantages to putting JavaScripts in externally referenced files. For starters, by moving JavaScripts out of your HTML pages you make your HTML pages smaller and easier to work with. In addition, you can reuse the JavaScripts stored as external files over and over again by referencing them from any number of HTML pages. This way if you create a script that you want to reference from multiple HTML pages, you can do so without having to embed the same script in different HTML pages over and over again. As a bonus, should you ever want to modify the functionality of an externally stored script, you may do so without having to visit every HTML page where you would otherwise have embedded it.

NOTE
References to external JavaScripts can be placed in either the head or the body section of the HTML page.There is no limit to the number of external references that you can make, and there is no limit to the number of statements you can place in an external JavaScript.
Placing JavaScript in an HTML Tag
JavaScript can also be placed within HTML tags, as shown in the following example.

<BODY onLoad=document.write(“Hello World!”)> </BODY>
In this example, the JavaScript onLoad=document.write(“Hello World!”) statement has been added to the HTML <BODY> tag. This particular JavaScript statement tells the browser to write the enclosed text when the browser first loads the HTML page.

Placing small JavaScript statements inside HTML tags provides an easy way to execute small pieces of JavaScript code. Of course, this option really is beneficial only when executing small JavaScript statements and is impractical for larger JavaScript statements or situations that required multiple lines of code. However, as you will learn tomorrow, you can also trigger the execution of JavaScripts embedded in an HTML page’s head section by embedding calling statements inside HTML tags.

NOTE
You may have noticed the unusual spelling of the word onLoad in the previous example.The L in the middle of the word is capitalized, and rest of the word is in lowercase letters.This type of notation is known as camelback notation.This is a perfect example of JavaScript’s case sensitivity. If you change the capitalization of this word in any way, you’ll get an error when you run your JavaScript.

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published.