JavaScript is the glue that makes the
whole Ajax operation possible. And having good understanding on
how JavaScript works is important for writing well-functioning Ajax
applications. In this hands-on lab, you will learn various
capabilities of
JavaScript language by writing and running JavaScript code.
Expected duration: 90-120 minutes
This hands-on lab assumes you have some basic knowledge of, or programming experience with, the following technologies.
In this exercise, you are going to see
various examples of JavaScript code and then modify them and see the
result.
The goal of this exercise is to
understand when JavaScript code gets executed. If the code is
within a function, it gets executed only when the function gets
invoked,
typically as an event handler. If the code is outside of a function, it
gets executed when a page that contains the code is loaded.
0. Start NetBeans IDE.
1. Open samplejavascripts
NetBeans project.
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> // The alert message below gets executed when the page is loaded alert("Hello Boston! This message gets displayed when a page is loaded."); // If alert("Hello world!!") below had not been written within a // function, it would have been executed when page is loaded. // Since it is within a function, it will be invoked only when // displaymessage() function is invoked. function displaymessage() { alert("Hello World! This message is within a function and gets displayed only when the function is called.") } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" > </form> </body> </html> |
4. Right-click function.html and select View. (Figure-1.11 below) This
will let the browser to load the function.html.

Figure-1.11: function.html
5. Observe that the browser gets
displayed with the "Hello Boston! This
message gets displayed when a page is loaded" alert
message. (Figure-1.12
below) (You might have to make the browser appear on the front
screen if it behind other windows.)
6. Click OK to close the alert
message.

Figure-1.12: JavaScript code outside a function gets executed when a
page is loaded
7. Click Click
me! button. (Figure-1.13 below) This will invoke displaymessage() function.

Figure-1.13: Clicking the button invokes an event handler
8. Observe that "Hello World! This
message is within a function and gets displayed only when the function
is called" alert message gets displayed. (Figure-1.14 below).
9. Click OK to close the alert message.

Figure-1.14: JavaScript code within a function gets executed
10. For your own exercise, please do the
following tasks. When you make a change, please make sure you
save the change first by selecting File
from the
top-level menu
and Save before selecting View.
The goal of this exercise is to learn how
onblur event occurs, which an
event handler is invoked by the event.
| <html> <head> <script type="text/javascript"> // Event handler of the first onBlur event function upperCase() { var x=document.getElementById("fname").value; document.getElementById("fname").value= "Uppercase -" + x.toUpperCase(); } // Event handler of the second onBlur event function lowerCase() { var y=document.getElementById("lname").value; document.getElementById("lname").value= "Lowercase -" + y.toLowerCase(); } </script> </head> <body> Enter your first name: <!-- onblur event is generated when the input form field loses the focus --> <input type="text" id="fname" onblur="upperCase()"> <br> Enter your last name: <!-- onblur event is generated when the input form field loses the focus --> <input type="text" id="lname" onblur="lowerCase()"> </body> </html> |
2. Right-click onBlurevent.html and select View.
3. Observe that the browser gets
displayed with two input form fields.
4. Enter your first name, for example, Sang, into the first input form
field. (Figure-1.22 below)

Figure-1.22: Executing onBlurevent.html
5. Press Tab
key or move your mouse outside of the input form field.
This
will cause onblur event to be
generated and the upperCase()
function will be invoked as an event handler. You should see the first
name you typed is converted into upper case letters. (Figure-1.23 below)

Figure-1.23: Effect of the onblur event
6. Enter your last name, for
example, Shin, into the second
input form field.
7. Press Tab key or move your
mouse outside of the input form field. This
will cause onblur event to be
generated and the lowerCase()
function will be invoked as an event handler. You should see the last
name you typed is converted into lower case letters. (Figure-1.24
below)

Figure-1.24: Effect of the onblur event
8. For your own exercise, please do the following tasks. When you make a change, please make sure you save the change first by selecting File from the top-level menu and Save before selecting View.
The goal of this exercise is to learn how
onclick event occurs and an
event handler is invoked as a result.
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>onClick event</title> <SCRIPT LANGUAGE="JavaScript"> // Event handler of onclick event function testResults (form) { var TestVar = form.inputbox.value; alert ("You typed: " + TestVar); } </SCRIPT> </head> <body> <FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR> <INPUT TYPE="text" NAME="inputbox" VALUE=""><P> <!-- When the button is clicked, onclick event is generated --> <INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)"> </FORM> </body> </html> |


The goal of this exercise is to learn how
onfocus event occurs and an
event handler is invoked as a result. The onfocus event gets generated when
an element gets a focus.
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> // Event handler of onfocus event function upperCase() { document.getElementById("fname").value= "Enter your first name"; } // Event handler of onfocus event function lowerCase() { document.getElementById("lname").value= "Enter your last name"; } </script> </head> <body> Enter your first name: <!-- onfocus event is generated when the input form field gets a focus --> <input type="text" id="fname" onfocus="upperCase()"> <br> Enter your last name: <!-- onfocus event is generated when the input form field gets a focus --> <input type="text" id="lname" onfocus="lowerCase()"> </body> </html> |

6. For your own exercise, please do the following tasks. When you make a change, please make sure you save the change first by selecting File from the top-level menu and Save before selecting View.
The goal of this exercise is to learn how
onsubmit event occurs and an
event handler is invoked as a result. The event handler performs
the input validation.
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> // This function gets called when onsubmit event occurs. // This function performs input form validation. function validate() { var at=document.getElementById("email").value.indexOf("@") var age=document.getElementById("age").value var fname=document.getElementById("fname").value submitOK="true" if (fname.length>10) { alert("The name must be less than 10 characters") submitOK="false" } if (isNaN(age)||age<1||age>100) { alert("The age must be a number between 1 and 100") submitOK="false" } if (at==-1) { alert("Not a valid e-mail!") submitOK="false" } if (submitOK=="false") { return false } } </script> </head> <body> <!-- onsubmit event occurs when the submit button is clicked --> <form action="tryjs_submitpage.html" onsubmit="return validate()"> Name (max 10 characters): <input type="text" id="fname" size="20"><br /> Age (from 1 to 100): <input type="text" id="age" size="20"><br /> E-mail: <input type="text" id="email" size="20"><br /> <br /> <input type="submit" value="Submit"> </form> </body> |

6. For your own exercise, please do the following tasks. When you make a change, please make sure you save the change first by selecting File from the top-level menu and Save before selecting View.
The goal of this step is to explore other
events on your own. Please see list
of events from this link. Try to write event handlers for the
events you have not tried in this exercise.
In this exercise, you learned when
JavaScript code gets executed. You also learned how various
events are generated and how corresponding event handlers are invoked.
In this exercise, you are going to create
your own JavaScript objects using the using various schemes.
The goal of this exercise is to learn how
to create a direct instance of JavaScript object and how to add
properties and methods to it.
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> // This is a function that is added as a method of a JavaScript object function myMethod(){ alert("myMethod is invoked!"); } </script> </head> <body> <h1> Example of creating a JavaScript object directly</h1> <script type="text/javascript"> // Create a direct instance of JavaScript object and add properties personObj=new Object(); personObj.firstname="John"; personObj.lastname="Doe"; personObj.age=50; personObj.eyecolor="blue"; // This is equivalent to println statement of Java programming language document.write(personObj.firstname + " is " + personObj.age + " years old.") // Add a method personObj.myMethod=myMethod; // Invoke a method personObj.myMethod(); </script> </body> </html> |

| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> // This is a function that is added as a method of a JavaScript object function myMethod(){ alert("myMethod is invoked!"); } // This is a my own function that is added as a method of a JavaScript object function myOwnMethod(){ alert("myOwnMethod is invoked!"); } </script> </head> <body> <h1> Example of creating a JavaScript object directly</h1> <script type="text/javascript"> // Create a direct instance of JavaScript object and add properties personObj=new Object(); personObj.firstname="John"; personObj.lastname="Doe"; personObj.age=50; personObj.eyecolor="blue"; // This is equivalent to println statement of Java programming language document.write(personObj.firstname + " is " + personObj.age + " years old.") // Add a method personObj.myMethod=myMethod; // Invoke a method personObj.myMethod(); // Create my own direct instance of JavaScript object and add properties addressObj=new Object(); addressObj.street="5 Dreamland"; addressObj.city="Seoul"; addressObj.country="Korea"; addressObj.zip=12345; document.write( "My own address is " + addressObj.street + " " + addressObj.city + " " + addressObj.country + " " + addressObj.zip ) // Add a method addressObj.myOwnMethod=myOwnMethod; // Invoke a method addressObj.myOwnMethod(); </script> </body> </html> |
The goal of this exercise is to learn how
to create a JavaScript object through a template.
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> // This is a function that is added as a method of a JavaScript object function myMethod(name){ alert("myMethod is invoked! The name of the person is " + name); } </script> </head> <body> <h1> Example of creating a JavaScript object through a template</h1> <script type="text/javascript"> // Define a template. Note that a template is just a function. // The properties are needed to be initialized with this. function person(firstname,lastname,age,eyecolor){ this.firstname=firstname this.lastname=lastname this.age=age this.eyecolor=eyecolor } // Create a person object myFather=new person("John","Doe",50,"blue") document.write(myFather.firstname + " is " + myFather.age + " years old.") // Add a method myFather.myMethod=myMethod; // Invoke a method myFather.myMethod(myFather.firstname); </script> </body> </html> |
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> // This is a function that is added as a method of a JavaScript object function myMethod(name){ alert("myMethod is invoked! The name is " + name); } </script> </head> <body> <h1> Example of creating a JavaScript object through a template</h1> <script type="text/javascript"> // Define a template. Note that a template is just a function. // The properties are needed to be initialized with this. function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; } // Create a person object myFather=new person("John","Doe",50,"blue"); document.write(myFather.firstname + " is " + myFather.age + " years old."); // Add a method myFather.myMethod=myMethod; // Invoke a method myFather.myMethod(myFather.firstname); // Create another person object myMother=new person("Sallie", "Lowe", 56, "brown"); // Invoke the same method. This will not work. myMother.myMethod(myMother.firstname); </script> </body> </html> |

| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> // This is a function that is added as a method of a JavaScript object function myMethod(name){ alert("myMethod is invoked! The name is " + name); } </script> </head> <body> <h1> Example of creating a JavaScript object through a template</h1> <script type="text/javascript"> // Define a template. Note that a template is just a function. // The properties are needed to be initialized with this. function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; } // Create a person object myFather=new person("John","Doe",50,"blue"); document.write(myFather.firstname + " is " + myFather.age + " years old."); // Redefine the template function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; //Add a method this.myMethod=myMethod; } // Invoke a method myFather.myMethod(myFather.firstname); // Create another person object myMother=new person("Sallie", "Lowe", 56, "brown"); // Invoke the same method. This should not work. myMother.myMethod(myMother.firstname); </script> </body> </html> |

In this exercise, you have learned how to
create your own JavaScript objects and how to access the properties and
methods of them.
In this step, you are going to create
myOwnJavaScript.js file in the top directory.
1. Right click samplejavascripts->Web Pages and
select New->File Folder.
2. If you have installed NetBeans JavaScript editor plug-in, you can
select the JavaScript under Categories and JSTemplate.js from the File Type. (Figure-3.11 below)

Figure-3.11: Create new JavaScript file
Trouble-shooting:
If you don't see the JavaScript under Categories, it is because you
have not installed JavaScript plug-in which was mentioned above.
3. Observe that New JSTemplate.js dialog box appears.
4. For the File Name: field,
type in myOwnJavaScript.
(Figuire-3.12 below) Click Finish button.
Since you are going to create it under the top directory, take the
default value of Folder field.

Figure-3.12: New JSTemplate.js dialog box
5. Observe that myOwnJavaScript.js file node appears
on the left under Web Pages and a blank contents of the myOwnJavaScript.js is displayed in
the source editor on the right.
6. Replace the blank contents of the myOwnJavaScript.js with the code
shown in Code-3.13 below. Note that you do not have to wrap the
JavaScript code with the <script ..>..</script> tag.
| // This is a
function that is added as a method of a JavaScript object function myMethod(name){ alert("myMethod is invoked! The name is " + name); } |
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <!-- ------------- Code fragment that is removed ------------------------ <script type="text/javascript"> // This is a function that is added as a method of a JavaScript object function myMethod(name){ alert("myMethod is invoked! The name is " + name); } </script> -----------------------------------------------------------------------------> <script language="JavaScript" type="text/javascript" src="myOwnJavaScript.js"> </script> </head> <body> <h1> Example of creating a JavaScript object through a template</h1> <script type="text/javascript"> // Define a template. Note that a template is just a function. // The properties are needed to be initialized with this. function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; } // Create a person object myFather=new person("John","Doe",50,"blue"); document.write(myFather.firstname + " is " + myFather.age + " years old."); // Redefine the template function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; //Add a method this.myMethod=myMethod; } // Invoke a method myFather.myMethod(myFather.firstname); // Create another person object myMother=new person("Sallie", "Lowe", 56, "brown"); // Invoke the same method. This should not work. myMother.myMethod(myMother.firstname); </script> </body> </html> |
8. Select File from top-level menu and select
Save All.
9. Right-click createobjectbytemplate.html
and select View.
10. Observe that the browser gets
displayed with properties of the first JavaScript object is displayed
and a method of it is also invoked. (Figure-2.22 above) Click OK.
11. Observe that the browser gets
displayed with properties of the first JavaScript object is displayed
and a method of it is also invoked. (Figure-2.34 above) Click OK.
In this step, you are going to create myOwn2ndJavaScript.js file in a
subdirectory.

6. Observe that myOwn2ndJavaScript.js file node
appears under myOwnSubDirectory
on the left under Web Pages and a blank contents of the myOwn2ndJavaScript.js is displayed
in the source editor on the right.
7. Replace the blank contents of the myOwn2ndJavaScript.js
with the code
shown in Code-3.22 below. Note that you do not have to wrap the
JavaScript code with the <script ..>..</script> tag.
| // This is a
function that is added as a method of a JavaScript object function my2ndMethod(age){ alert("my2ndMethod is invoked! The age is " + age); } |
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script language="JavaScript" type="text/javascript" src="myOwnJavaScript.js"> </script> <script language="JavaScript" type="text/javascript" src="myOwnSubdirectory/myOwn2ndJavaScript.js"> </script> </head> <body> <h1> Example of creating a JavaScript object through a template</h1> <script type="text/javascript"> // Define a template. Note that a template is just a function. // The properties are needed to be initialized with this. function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; } // Create a person object myFather=new person("John","Doe",50,"blue"); document.write(myFather.firstname + " is " + myFather.age + " years old."); // Redefine the template function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; //Add a method this.myMethod=myMethod; //Add a 2nd method this.my2ndMethod=my2ndMethod; } // Invoke a method myFather.myMethod(myFather.firstname); // Create another person object myMother=new person("Sallie", "Lowe", 56, "brown"); // Invoke the same method. This should work. myMother.myMethod(myMother.firstname); // Invoke the 2nd method myMother.my2ndMethod(myMother.age); </script> </body> </html> |

In this exercise, you have learned how to create JavaScript file as a separate and how to access them from your JavaScript code.
| <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <script type="text/javascript"> if(window.navigator.javaEnabled()){ alert("JVM is present in your browser! Version is " + java.lang.System.getProperty("java.version"));} else{ alert("JVM is NOT present in your browser!"); } </script> </body> </html> |
2. Right-click detectjvm.html
and select View.
3. Observe that the browser gets
displayed with an alert message, which shows if JVM is present or not
and the version number. (Figure-4.22 below)

Figure-4.22: JVM is present or not
Trouble-shooting:
The above code is working with Firefox and Netscape browser but does
not seem to be working with IE. If you know the solution, please
let me(sang.shin@sun.com) know.
(According to Joh Boles - The problem is that IE does not support
LiveConnect like almost all other browsers. The only way that I
know of to get the JavaScript code to work is to embed an applet and
then use it to communicate the JVM version and other Java
properties. This is how we do it for our application when the IE
browser is used.)