WT Unit 4
WT 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
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);
Lengthalert(document.forms[2].length);
Elementsalert(document.forms[2].elements[
0].name);
FORM METHOD INCLUDE
Submit()
Form events include:
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.
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:
<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.