Professional Documents
Culture Documents
JavaScript
• An Object Based Scripting language similar in Concept to
VBScript i.e., it is an interpreted language and supports
DOM. Thus use of window, document, form, navigator,
location objects is similar to VBScript.
• Syntax is similar to C language (has less things in common
with Java Language).
• Arrays and Collections use [ ] brackets instead of ( ).
• Unlike VBScript, JavaScript is case Sensitive.
• Very popular for Client-side Scripting since both Netscape
and Internet Explorer support JavaScript.
• There are a few versions of this language e.g., Microsoft
version is referred to as JScript. There is also a standard
version referred to as ECMAScript.
• JavaScript counts time in milliseconds. The reference point is
January 1, 1970.
• Variables do not need to be declared but it is a good practice to
do so. var is used to declare variables (similar to dim in VBScript).
• Client side Javascript code can be placed in HTML
Comments inside a <SCRIPT> tag.
• The default scripting language in a browser is JavaScript.
• + is used to catenate strings.
• Event names are similar to VBScript such as onClick for
button clicks and onBlur for loosing focus from an element.
• The element object properties are also similar to VBScript
such as .value for reading data from a text box, .checked to
see if a check box is checked etc..
• JavaScript provides a set of Math functions such as
Math.random( ), Math.sine( ), Math.floor(.), Math.ceil( ) etc..
• Each statement in JavaScript should be terminated by a ; (as
is done in C language)
• Parantheses are required in calling a function.
Example:
111
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
function cmdSubmit_OnClick() {
var i, strChoice ;
for (i = 0; i < window.frmData.optDinnerItem.length; i++) {
if (window.frmData.optDinnerItem[i].checked==true)
strChoice = window.frmData.optDinnerItem[i].value + "\n" ;
}
for (i = 0; i < window.frmData.optDessertItem.length; i++) {
if (window.frmData.optDessertItem[i].checked == true)
strChoice = strChoice + window.frmData.optDessertItem[i].value + "\n" ;
}
alert (" Dinner Choices = " + "\n" + strChoice);
}
-->
</SCRIPT>
</HEAD>
<BODY>
<H2> <CENTER> Client Side Scripting </CENTER> </H2>
<H3> <CENTER> Using the Radio Button Control </CENTER></H3>
<H4> <CENTER> (Also referred to as Option Button sometimes) </CENTER> </H4>
<HR>
<H4> Choose a main Dinner Item </H4>
<FORM NAME=frmData>
<PRE>
Pizza <INPUT TYPE=RADIO NAME=optDinnerItem VALUE="Pizza" CHECKED>
Steak <INPUT TYPE=RADIO NAME=optDinnerItem VALUE="Steak">
Fish <INPUT TYPE=RADIO NAME=optDinnerItem VALUE="Fish">
<HR>
<H4> Choose a Dessert </H4>
Chocolate Cake <INPUT TYPE=RADIO NAME=optDessertItem VALUE="Chocolate Cake"
CHECKED>
Cheese Cake <INPUT TYPE=RADIO NAME=optDessertItem VALUE="Cheese Cake">
Icecream Sundae <INPUT TYPE=RADIO NAME=optDessertItem VALUE="Icecream Sundae">
<HR>
}
-->
</SCRIPT>
</HEAD>
<BODY>
<H2> <CENTER> Client Side Scripting </CENTER> </H2>
<H3> <CENTER> Reading Data from the List Box Control </CENTER></H3>
<H4> <CENTER> (Use SIZE attribute to change a drop down to a List Box)
</CENTER> </H4>
<H4> <CENTER> (Use MULTIPLE attribute to allow multiple Selections)
</CENTER> </H4>
<HR>
<H4> Choose one or more Fruits </H4>
<FORM NAME=frmData>
<PRE>
<SELECT NAME=lstFruit SIZE=4 MULTIPLE>
<OPTION VALUE=Orange> Orange
<OPTION VALUE=Apple SELECTED> Apple
<OPTION VALUE=Bannana> Bannana
<OPTION VALUE=Mango> Mango
</SELECT>
<HR>
</HEAD>
<BODY>
<H2> <CENTER> Client Side Scripting </CENTER> </H2>
<H3> <CENTER> Reading Data from the Text Area </CENTER></H3>
<H4> <CENTER> (Text Area behaves like a multiline Text Box)
</CENTER> </H4>
<PRE>
Please provide Some Comments about Our Web Site:
116
<FORM NAME=frmData>
<TEXTAREA NAME=txaComments ROWS=5 COLS=40> Comments:
</TEXTAREA>
<HR>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
function cmdSubmit_OnClick() {
alert(" File Selected = " + "\n" +
window.frmData.filDatafile.value) ;
}
-->
</SCRIPT>
</HEAD>
<BODY>
<H2> <CENTER> Client Side Scripting </CENTER> </H2>
<H3> <CENTER> Collecting File Name from FILE type INPUT Control </CENTER></H3>
<PRE>
Click on the Button to Show File Open Dialog:
<FORM NAME=frmData>
<HR>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
117
function imgPrinter_OnClick() {
alert(" Image Type Button Attributes = " + "\n" +
"Source File = " + window.imgPrinter.src + "\n" +
"Name = " + window.imgPrinter.name);
}
-->
</SCRIPT>
</HEAD>
<BODY>
<H2> <CENTER> Client Side Scripting </CENTER> </H2>
<H3> <CENTER> An Image Type SUBMIT Button </CENTER></H3>
<PRE>
Click on the Image Button Below to Find its Properties:
<INPUT TYPE=IMAGE NAME=imgPrinter WIDTH=75 HEIGHT=75 SRC=hp.jpg
onClick="imgPrinter_OnClick();">
<HR>
</PRE>
</BODY>
<HTML>
-->
</SCRIPT>
</HEAD>
<BODY>
<H1> Web Adder with Data Validation </H1>
<PRE>
<FORM NAME=frmCalc method="" action="">
Value1: <INPUT NAME=val1>
Value2: <INPUT NAME=val2>
-----------------
Result: <INPUT NAME=RESULT>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
function Find_min(v1,v2,v3) {
// A simple function to be find the minimum of three numbers
var min ;
min = v1;
if (v2 < min) min = v2 ;
if (v3 < min) min = v3 ;
return min;
}
</HEAD>
<BODY>
<H2> <CENTER> Javascript Client Side Scripting </CENTER> </H2>
<H3> <CENTER> Writing Your own Functions - Finding Minimum </CENTER></H3>
<PRE>
119
Enter some numbers in the three text boxes, then Click on the
"Calculate Min" Button to Find the minimum of the three numbers entered:
<FORM NAME=frmMin>
Enter First Number : <INPUT TYPE=TEXT NAME=txtV1>
Enter Second Number : <INPUT TYPE=TEXT NAME=txtV2>
Enter Third Number : <INPUT TYPE=TEXT NAME=txtV3>
General Comments
Example: Collecting a response from the user by using the confirm method
of the window object. Note that the window object also provides the alert
function (which is similar to MsgBox in Vbscript) and the prompt function
(similar to InputBox in Vbscript)
<!-- Clientsc21J.htm -->
<!-- An example of a Client-side Javascript code that
collects a response from the user using confirm function.
window object supports alert, prompt and confirm methods. -->
<HTML>
<HEAD>
<TITLE> confirm Function example</TITLE>
<SCRIPT LANGUAGE="Javascript">
<!--
function cmdTestConfirm_OnClick () {
var ret;
ret = confirm("Save file before proceeding");
if (ret)
alert("You have opted to save");
else
alert("You have decided not to save");
}
-->
</SCRIPT>
</HEAD>
<BODY>
<H3> Test of the Confirm Function in Javascripts </H3>
<H2> Click on the Test Button to see Message Box </H2>
<FORM>
<INPUT NAME="cmdTestConfirm" Type="Button" VALUE="Test Confirm Function"
onClick="cmdTestConfirm_OnClick();">
</FORM>
</BODY>
</HTML>
121
<BODY>
<H3> Prompt function Example (method of the window object) </H3>
<H2> Click on the Test Button to see Prompt Box </H2>
<FORM>
<INPUT NAME="cmdTestInputBox" Type="Button" VALUE="Test Input Box"
onClick="cmdTestPrompt_OnClick();">
</FORM>
</BODY>
</HTML>
-->
</SCRIPT>
</HEAD>
<BODY>
<H3> Javascript String Function Example </H3>
<H2> Click on the Button below to do some String Processing </H2>
<HR>
<INPUT TYPE="button" NAME=cmdEnter VALUE="Test of String Functions"
onClick="cmdEnter_OnClick();">
</BODY>
</HTML>
Example:
<!-- Clientsc27J.htm -->
<!-- Use of Javascript Date Time Functions -->
<HTML>
<HEAD>
<TITLE> Date Time Functions Example </TITLE>
123
<SCRIPT LANGUAGE="Javascript">
<!--
function cmdTestDate_OnClick() {
var d1;
d1 = new Date();
alert("Time = " + d1.getHours() + ":" + d1.getMinutes()+ ":" + d1.getSeconds());
}
-->
</SCRIPT>
</HEAD>
<BODY>
<H3> Test of Javascript Date and Time Functions </H3>
<H2> Click Button to Invoke Date Time Functions </H2>
<FORM>
<INPUT NAME="cmdTestDate" Type="Button" VALUE="Test Date and Time"
onClick="cmdTestDate_OnClick();">
</FORM>
</BODY>
</HTML>
Example:
<!-- Clientsc29J.htm -->
<!-- Use of Javascript Date difference capabilities -->
<HTML>
<HEAD>
<TITLE> Date difference Example </TITLE>
<SCRIPT LANGUAGE="Javascript">
<!--
function cmdEnter_OnClick() {
var bd;
bd =new Date(frmBday.txtBday.value);
now = new Date();
var y1, m1, d1;
y1 = Math.floor((now - bd)/(1000 * 60 * 60 * 24 * 365)) ;// age in years
m1 = Math.floor((now - bd)/(1000 * 60 * 60 * 24 * 365) * 12) ;// age in months
d1 = Math.floor((now - bd)/(1000 * 60 * 60 * 24)) ;// age in days
alert("Your age in years = " + y1 + "\n" + "age in months = " + m1 + "\n" +
"age in days = " + d1);
}
-->
</SCRIPT>
</HEAD>
<BODY>
<H3> Test of JavaScript Date difference Capabilities </H3>
<HR>
<FORM Name=frmBday>
Please enter your Birthday :
<INPUT Name=txtBday value="">
<BR>
<INPUT NAME="cmdEnter" Type="Button" VALUE="Click to Calculate Age"
onClick="cmdEnter_OnClick();">
</FORM>
124
</BODY>
</HTML>
Example: Switch statement, onLoad event, Math functions.
<!-- Clientsc30J.htm -->
<HTML>
<HEAD>
<!-- Example of Window_Onload Event -->
<TITLE> A Colorful Start to the Web Page </TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
function window_onLoad() {
var colornum, BColor;
colornum = Math.round(Math.random() * 8);
alert(colornum);
switch(colornum) {
case 0 : BColor = "green"; break;
case 1 : BColor = "blue"; break;
case 2 : BColor = "yellow";break;
case 3 : BColor = "lime"; break;
case 4 : BColor = "aqua"; break;
case 5 : BColor = "silver"; break;
case 6 : BColor = "purple"; break;
case 7 : BColor = "orange"; break;
default : BColor = "red";
}
document.bgColor = BColor ;
}
-->
</SCRIPT>
</HEAD>
<BODY onLoad="window_onLoad();">
<H2> Possibly a A different Color each time the
document is loaded or refreshed </H2>
</BODY>
</HTML>
function txtEmail_OnBlur() {
var strEmail;
strEmail = frmFeedback.txtEmail.value ;
if (strEmail == "") {
125