Let’s start
with developing a simple JavaScript application that displays the text
“Hello World”. JavaScript can be saved in standalone files with the .js
extension or embedded directly into the HTML on your page. Use HTML's
script tag to let the browser know how to interpret your code as shown
in the code block below. To try this out on your own, use any basic text
editor, notepad for example, to write the code. Before you start writing
the code, you need to know that:
- JavaScript
is case sensitive and you need to put semi-colons at the end of each
JavaScript statement.
- You can
add comments to the code by using double slash (//). To add multi-line
comments adding "/*" at the beginning line "*/” at the ending line.
- You should
also add comments around the script code, as shown in the code sample
to ensure that the old browsers that do not understand the JavaScript
code simply ignore the script and do not generate an error.
<script
type="text/javascript">
<!--
//JavaScript
goes here
//-->
</script> |
Perform the
following steps to write the application:
-
Open
the Notepad by clicking Start->Programs->Accessories->Notepad
-
Add the
following code to it:
.
<html>
<body>
//
Script tag will
tell the browser that the scripting language used is Java Script
<script
type="text/javascript">
//
This function
will take a string parameter and display it on the web page
document.write("Hello
World!");
</script>
</body>
</html> |
-
Save
the file with .html extension
-
Double
click the file. The output appears, as shown in Figure.
|