Basic JavaScript with
Examples
A tutorial with reusable
code snippets
By Reaz Hoque
//
originally appeared in IDM v2n13 (June 1997)
Since the day Microsoft built
support for JavaScript into Internet Explorer 3.0, Netscape's client-side
language has become the de facto standard for enhancing web pages
at the browser.
In this full-length excerpt from Practical
JavaScript Programming, author Reaz Hoque explains the basics
of client-side scripting. He also gives you some neat scripts that can
enhance your web pages, making them impressively interactive. Important
code snippets available in this article include a browser detection
script, capable of offering tailored content based on broswer version
or type. For instance, it's important (unless you like crashing
visitors' computers) to only serve JavaScript 1.1 code to Netscape Navigator
3.0 or later.
By simply cutting and pasting the code examples below, you'll be well
on your way to making your web site a more valuable, and more enjoyable,
place to visit.
Your first JavaScript script
In any language, the programmer needs to find out
how to output text into a document. In order to output text in JavaScript
you must use write() or writeln(). Here's an
example:
Writing text on a document
<HTML>
<HEAD>
<TITLE>Writing text on a document</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!-- Hiding the code
document.write("Welcome to Applications in JavaScript!");
// done hiding -->
</SCRIPT>
</BODY>
</HTML>
Test This Example
If you're familiar with JavaScript syntax, this script should be easy
for you to understand. You will notice that we have included the code
in between <SCRIPT> and </SCRIPT>
tags. We made sure the document object write is in lowercase
as JavaScript is case sensitive.
Now, you might be wondering what the difference is between write
and writeln. Well, while write just outputs
a text, writeln outputs a line feed after the text output.
Date and time
You probably have seen many Web sites that incorporate
a clock. Before JavaScript, you had to use CGI to have the current date
and time on a page. Now, creating clocks and the date is easy and can
make your site look cool. Using this example, you also can create custom
greetings for your visitor, depending on the time of day. For example,
you can have a message saying, "It's 12:00 a.m.. Go to bed!"
If you look at the Netscape's JavaScript documentation, you will know that
there are two built-in functions in JavaScript that let you display date and
time. The following example will display the current time and date
in a text box with an option to stop the time.
<HTML>
<HEAD>
<TITLE>Showing date and time on a document</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--Hiding the code
var show_time=false;
var timerID=null;
function stop(){
if (show_time){
clearTimeout(timerID);
document.clock.date_time.value=" ";
}
show_time=false;
}
function start(form){
var today=new Date();
var display_value =" Time= " + today.getHours()
if(today.getMinutes() < 10){
display_value+=":0" + today.getMinutes();
}
else{
display_value+=":" + today.getMinutes();
}
if (today.getSeconds() < 10){
display_value+=":0" + today.getSeconds();
}
else{
display_value+=":" + today.getSeconds();
}
if(today.getHours()>=12){
display_value+=" P.M."
/* Here we have a variable called mygreeting where
you can store somthing like this:
mygreeting ="Good Morning!"; */
}
else{
display_value+=" A.M."
/* Now set mygreeting to:
mygreeting ="Good Afternoon!"; */
}
display_value += " Date= " + (today.getMonth()+1) + "/" +
today.getDate() + "/" + today.getYear();
document.clock.date_time.value=display_value;
timerID=setTimeout("start()",100);
show_time=true;
/* Here have an alert() method do the following:
alert(mygreeting); */
}
//done hiding-->
</SCRIPT>
</HEAD>
<BODY BGCOLOR=white Text=Red Link=Green onLoad=stop()>
<center>
<H2>Displaying Date and Time</H2>
<FORM name=clock>
<INPUT type="text" name="date_time" size=35 value=" "><br>
<INPUT type="button" name="show_now" value="Display" onClick=start()>
<INPUT type="button" name="clear_now" value=" Clear " onClick=stop()>
</center>
</FORM>
</BODY>
</HTML>
Test This Example
In this script, we have two functions (stop() and start())
for our two buttons Display and Clear. When the button Display is pressed,
the start() function is called, and when the button Clear
is pressed, the stop() function is called. Both of these
functions are called using the onClick event handler.
Now the stop() function simply clears the setTimeout
method that is used in the start() function. This function
also clears the text box that displays the time and date. Finally, the
function puts a false value to the Boolean variable show_time.
In start() function, a new instance of date using the
statement new is created. Then a variable display_value
was defined so that we can export the date and time value in the text
box later. Remember that we are using the display_value
as a string variable. Now we have to make sure that if the seconds and
minutes are less than 10, they are both displayed as single digits.
The if...else statement is used for that purpose.
In order to display a.m. or p.m., another if...else statement
is used to check for a value more than or equal to 12. So, if the time
value is more than 12, display_value would then hold "p.m.,"
otherwise it would hold "a.m." Lastly, the value of the day, month,
and the year is added in the variable. Now it's time to display the
data into the text box. This is done with the following code:
document.clock.date_time.value=display_value;
The above code takes the value of display_value and puts
it in the property called value of the text box named date_time.
The word document is used to insure that we are indicating the current
document. Lastly, the variable timerID is set and show_time
is set to true. Notice that timeID uses the setTimeOut()
function with the parameters start() and 100. The reason
why 100 is passed in is because the function start() is within
one tenth of the second.
Page 1 2
3