0% found this document useful (0 votes)
89 views81 pages

WT Unit 4

This document provides an overview of JavaScript and VBScript. It discusses the need for scripting languages, JavaScript language elements like identifiers, literals, variables, expressions, keywords and operators. It covers JavaScript statements like loops, conditional statements, and functions. Objects in JavaScript like Window, Document, Forms are explained. Other objects like Date, Math, String and Arrays are also covered. The document then provides a brief introduction to VBScript and how to embed VBScript code in an HTML document.

Uploaded by

mahalakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views81 pages

WT Unit 4

This document provides an overview of JavaScript and VBScript. It discusses the need for scripting languages, JavaScript language elements like identifiers, literals, variables, expressions, keywords and operators. It covers JavaScript statements like loops, conditional statements, and functions. Objects in JavaScript like Window, Document, Forms are explained. Other objects like Date, Math, String and Arrays are also covered. The document then provides a brief introduction to VBScript and how to embed VBScript code in an HTML document.

Uploaded by

mahalakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 81

UNIT-4

JAVASCRIPT& VBSCRIPT
JAVASCRIPT
 Introduction
 Quicker & simple language for enhancing
web pages and servers.
 Javascript enables to embed small program
codes in an HTML page.
1.NEED OF A SCRIPTING
LANGUAGE
 Javascript enables to write small scripts that
execute on users browsers rather than on the
server.
 JavaScript introduces increased functionality
for web browser in the form of java applets,
plug-ins and DHTML elements.
 It provides complete set of built-in functions
and commands.
2.LANGUAGE ELEMENTS
IDENTIFIERS.
Javascript uses to identify a variable,method
or an object

Identifiers

Literals Variables
CONTINUED…
 Literals have fixed values.
 Variables have different values during
execution.
 Literals & variables have several types
supported in javascript
 They are Integers, Floaing point numbers,
Booleans
2.2EXPRESSIONS
 It is a statement that is evaluated to a value.
 Result can be any type.
 It contains
 Variables
 Literals
 Operators
 Ex: y=67;
str=“Good morning”;
val=x+y*b;
2.3 JAVASCRIPT KEYWORDS
 Keywords are reserved for specific purpose only
 Cannot be used as variables or constants.
 Keywords are

abstract
Boolean
char
break
byte
case
catch
class
Const etc….
OPERATORS
 Operators are commads which perform
opertions on variables and produce result.
 Unary & binary are 2 types
 ASSIGNMENT OPERATORS
 It takes result of an expression and assign to
variable.
 Example x=a+b;
MATH OPERATORS
 + addition
 - subtraction
 * multiplication
 / division

Example
X=a+b;
U=y*x/a;
Other operators are
 X=++a;
 X=a++;
 X=--a;
 X=a--
LOGICAL OPERATOR
 Three logical operator
 < less than
 > greater than
 <= less than and equal
 >=greater than and equal
 == equal
 != not equal
COMPARISON OPERATORS
 < less than
 > greater than
 <= less than and equal
 >= greater than and equal
 == equa
 != not equal
STRING OPERATOR
 Comparison operator can be used with
strings.
 For concatenating two strings ‘+’ is used.
 Str=“good”+”day”
2.5 STATEMENTS
 1.For loop

•Initial Expression

•Condition

•Update Expression
CONTINUED…
 Syntax
For ([initial];[condition];[update])
{
Statements
}
2. for….in loop
Iterates the variable overall the properties in an object.
Syntax
For(var in obj)
{
Statements
}
CONTINUED…
 3. While and do…..while loop
 It contains a condition and block of statements.
 Syntax

While(condition)
{
Statements;
}
Do
{
Statements;
}
While(condition);
CONTINUED…
 Break statement
 Used to terminate the current iteration in
the while and loop.
 Syntax

break;
 Continue statement
 Used to continue the loop for next iteration
without executing the statements following
the continue statement.
 Syntax : continue
CONTINUED…
 If….else statement
It execute block1 statement when true else execute
optional block.
Syntax
If(condition)
{
Block1;
}
[else
{
Block2;
}
]
CONTINUED…
 Var statement
Used to declare the variables.
Initialization can be done in statement itself
Syntax
Var name1 [=value], var name2 [=value],….
Var x=0, y=1
2.6 FUNCTIONS
 It is a kind of mini program that forms part of
larger program
 It consist of one or more statements.
 They are given a unique name so it can called from
elsewhere in program.
 It allows to have parameters or arguments and
return a value to statement.
function name()
{
statement;
statement;
Statement }
CONTINUED…
 All the statement except the last statement
must be followed by semicolon.
 They also written in a way that they accept
parameters, do process & return result.
 All variable declared in function are known
only in function where they defined are
called local variables.
EXAMPLE
<html>
<head>
<title>javascript>
</title>
</head>
<body>
<script language=“javascript”>
Function add(a,b)
{
Var p;
P=a+b;
Return p;
}
Document.writeln(add(5,3));
</body>
</html>
3.OBJECTS OF JAVASCRIPT
 Javascript is an object oriented language.
 Set of variables,functions etc that
encapsulate data and methods.
 Also provides Objects that encapsulate
various attributes and methods in script.
 The objects are arranged into a hierarchy
known as Document Object Model
3.1 WINDOW OBJECT
 It is fundamental object in browser.
 It represents browser window in which
document appears.
 Properties are

Status -> window.status=“Hi!”;


Location-> alert(window.length);
Parent -> var parentWindow=window.parent;
Alert(parent window.length);
Top-> var topWindow=window.top;
Alert(topWindow.length);
CONTINUED….
 The methods include
 alert()alert(“Hi”);
 confirm()var response=confirm(“surely
want to delete file?”);
Alert(response);
 prompt() prompt(message_string,
default_response_string)
 Open()open(url_string,name_string,
parameter_string)
 Close()window_name.close()
CONTINUED…..
 onLoad()<body onLoad=“displayWelcome()”>
 onUnload() <body onLoad=“displayclose()”>

3.2 The Document Object


bgColordocument.bgColor=“green”;
fgColordocument.fgColor=“blue”;
linkColordocument.linkColor=“brown”;
alinkColordocument.alinkColor=“lightred”;
vlinkColordocument.vlinkColor=“darked”;
Titledocument.title=“This title is displayed
now”;
Formsforms[index-number]
DOCUMENT METHODS INCLUDE
 write() allow a string of text to be written to
the document.
 Example

Document.write(“<h1>Hello</h1>”);
Document.write(“<P>welcome to this new
page</p>”);
Document.write(“<p>To return to the recent
page,”);
Document.write(“<a href=‘NewDoc.html’>click
here </a></p>”);
3.3 FORMS OBJECT
When a form is created in an HTML document
using the<form> and /form>tags, a form
object is created automatically with
properties,methods and events.
Form properties include:
Name<form name=“myForm”>
Alert(document.forms[2].name);
Method<form method=“POST”>
Alert(document.forms[2].method);
CONTINUED…
Action<form action=mailto:[email protected]
>
Alert(document.forms[2].action);
Lengthalert(document.forms[2].length);
Elementsalert(document.forms[2].elements[
0].name);
FORM METHOD INCLUDE
 Submit()
 Form events include:

onSubmit form onSubmit=“displayOK()”>


3.4 Text-boxes and text-areas
Properties include:
Name <input type=text name=“textbox1”>
Valuealert(document.forms[2].textbox1.valu
e);
CONTINUED…
 OnFocus event signal generated when
userclicks
 <input type=text
name=“textbox2”onFocus=“alertOnFocus()”>
 onBlur generated when user clicks outside
 <input type=text name=“textbox3”
onBlur=“alertOnBlur()”>
3.5 BUTTONS, RADIO-BUTTONS
AND CHECKBOXES
 Properties are
 Name defined in HTML<input>tag

<input type=button name=“button1”>


 ValueValue given to the button when it is
created.
Document.forms[2].button1.value=“new
value,new label”;
 Checkedused radio buttons and check
boxes but not in standard buttons.
CONTINUED…..
If(document.forms[2].checkbox1.checked==tru
e)
{
Alert(“checked”);
}
Else
[
Alert(“Not checked”);
};
METHODS INCLUDE..
 focus()document.forms[2].button2.focus();
 blur()document.forms[2].button2.blur();
 click()document.forms[2].button2.click();
 EVENT includes
 onClicksignal sent when the button is
clicked.
 <input type=button name=“button3”
value=“Click here” onClick=“alert(‘onClick
event received’)”>
 It cause alert dialog box when button is
pressed.
CONTINUED…
 onFocus<input type=button
name=“button4” value=“click Here”
onClick=“alert(‘This button is now the
default’)”>
 onBlursignal sent when button loses focus.

<input type=button name=“button5”


value=“Click Here” onBlur=“alert(‘this
button is no longer the default’)”>
CONTINUED…
 3.6 The select object
 selectedIndexalert(doument.forms[2].selec
tBox1.selectedIndex);
4.OTHER OBJECTS
 1.Date object
 It allows us to obtain current date7time.

Var myDataObject= new date;


Some methods are:
getFullYear()
getMonth()
getDate()
getDay()
getHours()
getMinutes()
getSeconds()
EXAMPLE
<html>
<head>
<title>example</title>
<script language=“JavaScript”>
var myDataObject=new Date;
var currentday=myDateObject.getDay();
alert(currentDay);
</script>
</head>
<body>
</body>
</html>
CONTINUED..
 4.2 Math Object
 Used for mathematical operations
 Methods are

sqrt(x)
log(x)
max(x,y)
min(x,y)
round(x)
ceil(x)
floor(x)
abs(x)
pow(x,y)
CONTINUED..
 4.3 string Object
 To manipulate string in different ways.

Var mystring=new string(“Hello world”);


It also include length as its property
Var
stringLength=document.form1.textbox1.valu
e.length;
CONTINUED..
 Methods are
charAt()returns character
indexOf()searches string
lastIndexOfsearches string of last occurences
substring()return position of string
substr()return position of string for specified
number
charCodeAt()Returns numerical value
fromCharCodereturn characters
tostring()Converts a number into a string
EXAMPLE
Var myString=“Hello world”;
document.write(mystring.italics());

bold() Formats the string with <b> and


</b>tags
sup() formats the string with <sup> and
</sup>tags
sub() formats the string with <sub> and </sub>
tags
5.ARRAYS
 Array is a set of variables that are grouped
together and given a single name.
var myarray=new
array(“sarah”,”patrick”,”june”,”trim”);
An empty array can be created
var myArray=new Array();
Number of elements in array can determined
using length property
alert(myArray.length);
Alert(myarray.valueOf()) etc
VBSCRIPT
 1.Introduction
 VBScript is an example of scripting language
 VBScript is a light version of microsoft’s
programming language Visual Basic.
2.EMBEDDING VBSCRIPT CODE
IN AN HTML DOCUMENT
 VBScript is inserted into an HTML document for the
Internet browser to interpret the VBScript by
reading HTML
<html>
<head>
</head>
<body>
<script type=“text/vbscript”>
Document.write(“hello”)
</script>
</body>
</html>
CONTINUED..
 To use a VBScript in a page, the section
should start with
<script language=“VBScript”>
Scripts placed in the head section.
<html>
<head>
<script type=“text/vbscript”>
Some statements
</script>
</head>
CONTINUED..
 Scripts are placed in the body section
<html>
<head>
</head>
<body>
<script type=“text/vbscript”>
Some statements
</script>
</body>
3.COMMENTS
 There are 2 ways to create line of comment
in VBScript.
<SCRIPT LANGUAGE=“VBScript”>
Rem this line is ignored. This a comment
statement
Document.Write(“Hello world”)
</script>
4. VARIABLES
 Program information can be stored.
 They remain unchangeable during program
execution.
 Example

dim name
name=some value
 Multiple variables can use by separating by
comma
dim fullname, email id,
dim address, city, state.
Once procedure is stoped variables are destroyed.
4.1 ARRAY VARIABLES
 To declare array variables parantheses()
following variable name are used.
dim names(3)
name(0)=“Tove”
name(1)=“jani”
name(2)=“stale”
MULTIPLE DIMENSIONS ARE DECLARED
dim table(4,6)
5.OPERATORS
1.Assignment Operator
 Gives value to variable using = operator

variableName=value
Example
<script Language=“VBScript”>
Dim salary
Salary=12.5
</script>
CONTINUED..
2.Numerical operator
To perform arithmetic operations on variables.
Operators
+ addition
 Subtraction

* multiplication
/ Division
Mod Remainder
- Negation etc…
CONTINUED..
 3. String Concatenation
 Allow to add different strings and create a
new one using & operator
 Value1&value2
 Example
EXAMPLE
 <Script Language=“VBScript”>
Option Explicit
Dim var1, var2
Var1=“hello”
Var2=“world”
Document.Write(“Full text”)
Document.Write(var1&””&var2)
</script>
6.PROCEDURES

VBScript procedures

Sub Function
6.1 SUBPROCEDURE
 Serious of statement enclosed by the sub and
end sub statement
 Compulsory include set of parantheses[()]
 It can perform actions but not return a
value.
CONTINUED..
 Syntax
Sub sub()
Some statements
End sub
Example
Sub DisplayFullText()
FullText=var1&””&var2
End Sub
CONTINUED..
 Keyword call can be used to call a sub
procedure.
 You must include the arguments between
parenthesis
Sub Result()
Dim hours as Double
Dim salary as Double
Hours=txtHours
Salary=txtSalary
Call CalcAnddisplaySalary(Hours,Salary)
End Sub
6.2 FUNCTION PROCEDURE
 Serious of function and end Function
statements enclosed by function and End
Function.
 It can return a value.
 Syntax

Function myfunc()
Somestatements
Myfunc=some value
End Function
EXAMPLE
Function CalculateArea(Radius)
CalculateArea=Radius+radius * 3.14159
End Function
Function can be called as
area=CalculateArea(5)
7.CONDITIONAL STATEMENTS
 To perform different decisions
 If statement
 If…then…else statement
 If….then…elseif statement
 Select case statement
CONTINUED…
 If…then statement
Syntax is
If Condition Then
Statement
End If
Example
If i=10 then msgbox “hello”
i=i+1
End if
CONTINUED..
If…then…else statement
Syntax
If ConditionIsTrue Then
Expression1
Else
Expression2
End If
Example
If i=0 then
Msgbox “Hello”
Else
Msgbox “Goodbye”
End if.
CONTINUED..
 If…then…..elseif statement
Syntax
If Condition1 then
Statement1
Elseif condition2 then
Statement2
Elseif condition K then
Statement k
End if
CONTINUED..
 Select case
Syntax
Select case expression
Case expression1
Statement 1
Case expression2
Statement 2
Case expression K
Statement K
End select
8.LOOPING CONSTRUCTERS
 In VBScripts four looping statements
 For…Next statements
 For each…Next statement
 Do….Loop statement
 While….wend statements
CONTINUED..
 DO…Loop statements
Used to run a block of code when number of
repetitions are not known.
Do while Condition
Statement(s)
Loop
VBScript offers synta
Do
Statement(s)
Loop While Condition
CONTINUED..
 Loop will execute statement before the
condition
 Formula

Do
Statement(s)
Loop until condition
 Exited using Exit Do keyword
CONTINUED..
 While …Wend statements
 Uses following prototype:

For counter=Start To End


Statement(s)
Next
 For…Each ….Next statements

For each variable1 in variable2


Code
Next
9.OBJECTS AND VBSCRIPT
 Objects enhance the functionality .
 2 steps are
 Adding the object to web page using HTML
 Writing script procedures to respond to
events.
 Objectsare added to apge using <object>tag.
 The properties are configured using
<PARAM>tag
CONTINUED….
 Setting properties of the label control
 Example

<SCRIPT LANGUAGE=“VBScript”>
Sub cmdCalculatePay_onClick
Dim HoursWorked
Dim PayRate
Dim TotalPay
HoursWorked=InputBox(“Enter hours worked:”)
PayRate=InputBox(“Enter Pay Rate:”)
Total Pay=Hours worked* PayRate
Totalpay.caption=TotalPay
End sub
</SCRIPT>
10.COOKIES
 It cannot contain any potentially harmful
binary code.
 Cookie cannot read information from the
hard drive
 Nor can they publicize the personal
information to the world.
CONTINUED…
 ActiveX control might appear when added to
page.
<OBJECT ID=“TotalPay” WIDTH=60 HEIGHT=25”>
CLASSID=“CLSID:978C9E23-D4B0-11CE-BF2D-
00AA003F40D0”>
<PARAM NAME=“ForeColor” VALUE=“0”>
<PARAM NAME=“BackColor” VALUE=“16777215”>
<PARAM NAME=“Caption” VALUE=“”>
<PARAM NAME=“Size” VALUE=“1528;635”>
<PARAM NAME=“FontWeight” VALUE=“0”>
<PARAM NAME=“ForeColor” VALUE=“0”>
CONTINUED..
 List of Limitations placed on the cookies:
 A client machine cannot store more than 300 cookies.
 A client machine cannot store more than 20 cookies
from a single domain.
 Only cookies that have an expiry date associated with
them are stored on the client machine.Most cookies are
simpy lost when the browser is exited.
 A cookie is not a program .It is simply as ASCII text file.
 Cookies cannot obtain and contain personal
information.
 A cookie can be read only by the domain that created
it.
FEW EXAMPLES OF HOW THE
COOKIES COULD BE USED TO
 Maintain a list of selected items,quantities and
colors.
 Maintain a record of the number of times a user
has visited a particular site.
 Store the date when a user last visited a website
and highlight items that have changed since
his/her last visit.
 Maintain a user-customized color scheme for a
website.
 Maintain a basic information file for a user-that
is first name, preferences, and so on-to enable
to personalize the size for the user.
10.1 COOKIE VARIABLES
 Cookie values are stored as name=value pairs
delimitted with semicolon.
Domain
Syntax: domain=domain_name;
Path
Syntax:path=path;
Secure
Syntax:secure;
Expiry Date
Syntax: expires=date;
Day,dd-mm-yy hh:mm:ss GMT
10.2 CREATING A COOKIE
 When the browser is closed the cookie is deleted.
Example
<HTML>
<HEAD>
<TITLE>setting a cookie</TITLE>
<SCRIPT LANGUAGE=“vbscript”>
Dim varNmae
Dim Varvalue
Varname=“mycookie”
VarValue=Date()
Document.Cookie=Varname &”=“& VarVal &”;”
CONTINUED…
Sub cmdButton_OnClick
Alert Document.Cookie
End sub
</script>
<body><INPUT TYPE=“button”
NAME=“cmdbutton” VALUE=‘show cookie
value”
</body>
</html>
10.3 A COOKIE WITH MULTIPLE
VALUES
<HTML>
<HEAD>
<TITLE>
Setting a cookie</TITLE>
<SCRIPT LANGUAGE=“vbscript”>
Dim varName
Dim varVal
Dim VarName1
Dim VarVal1
Dim Exp
Exp=“expires=Wednesday,09-nov-1999 23:12:40 GMt”
VarName=“mycookie”
VarVal=Date()
VarName1=“name”
VarVAl1=“Hai”
CONTINUED…
Document.Cookie=VarName&”=“
varVal&”;”&VarName1&”=“&VarVal1&Exp
Sub cmdButton_OnClick
Alert Document.Cookie
End sub
</SCRIPT>
<BODY>
<INPUT TYPE=“Button” NAME=“cmdButton”
VALUE=“Get cookie Value”>
</BODY>
</HTML>
10.4 READING COOKIE VALUE
 To retrieve the cookie values.
 Value can got by manipulating a string that is
returned by Document.Cookie.
 find the variable name within the string &
from its position in the string the value is
extracted.

You might also like