Page 290 - Web Applications (803) Class 11
P. 290
Step 3: Save the above code with .html extension and then open this file in any web browser. The web browser
displays the alert message ‘Hello World’.
External JavaScript
JavaScript code can be written in a file and saved with the .js extension and this file can then be linked inside the
<HEAD> and </HEAD> tags of the HTML document in which we want to add this code. The SRC attribute of the
<script> tag allows to give path of the JavaScript file. The syntax to add external JavaScript is:
<HEAD>
<SCRIPT TYPE="text/javascript" SRC="D:\first.js">
</SCRIPT>
</HEAD>
Note that there are many other scripting languages, hence, it is recommended to use the TYPE attribute with the
<SCRIPT> tag and set its value to Text/JavaScript to specify that scripting language we are using.
Creating a Web Page with External Javascript
To use external JavaScript, follow the given steps:
Step 1: Open the Notepad and type the following line:
alert("Hello World");
Step 2: Save the file with .js extension.
Step 3: Open another Notepad and type the following code:
<HTML>
<HEAD>
<TITLE> My First JavaScript </TITLE>
<SCRIPT TYPE="Text/javascript" SRC="external.js">
</SCRIPT>
</HEAD>
<BODY>
This message box is displayed using external JavaScript.
</BODY>
</HTML>
Step 4: Save the file with .html extension and open the web page in a web browser. You will get the same output as
the previous web page.
288 Touchpad Web Applications-XI

