Page 181 - Web_Application_v2.0_C12_Fb
P. 181
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’.
Output of the preceding program is as follows:
External JavaScript
You can write JavaScript code in a file, save it with a '.js' extension, and then link it to an HTML document. To
do this, put the file path in the SRC attribute of the <script> tag, inside the <HEAD> and </HEAD> tags of
your HTML document.
The basic syntax is:
<HEAD>
<SCRIPT TYPE="text/javascript" SRC="D:\first.js">
</SCRIPT>
</HEAD>
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! This is an External JavaScript example.");
Step 2: Save the file with .js extension.
Step 3: Open another Notepad and type the following code:
<!DOCTYPE HTML>
<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.
JavaScript Part 2 179

