Client Side Scripting2
Client Side Scripting2
JavaScript
Introduction
JavaScript is a scripting language that will allow you to add
real programming to your web pages.
JavaScript is a client-side scripting language. This means the web
surfer's browser will be running the script. However, the name
JavaScript is owned by Netscape. Microsoft calls its version of the
language JScript. The generic name of the language is EcmaScript.
Browser Detection
Detecting the browser used by a visitor at your page.
Depending on the browser, another page specifically
designed for that browser can then be loaded.
Cookies
Storing information on the visitor's computer, then
retrieving this information automatically next time the
user visits your page. This technique is called "cookies".
Control Browsers
Opening pages in customized windows, where you specify
if the browser's buttons, menu line, status line or
whatever should be present.
Validate Forms
Validating inputs to fields before submitting a form.
An example would be validating the entered email
address to see if it has an @ in it, since if not, it's not a
valid address.
Example:
<html>
<body>
<script type="text/JavaScript">
document. write("Hello World!") ;
</script>
</body>
</html>
+ Addition 2+4
- Subtraction 6-2
* Multiplication 5*3
/ Division 15 / 3
% Modulus 43 % 10
Comparison Operators
Comparisons are used to check the relationship between variables
and/or values. A single equal sign sets a value while a double equal
sign (==) compares two values. Comparison operators are used \
inside conditional statements and evaluate to either true or false.
Operator English Example Result
== Equal To x == y false
!= Not Equal To x != y true
< Less Than x<y true
> Greater Than x>y false
Less Than or Equal
<= x <= y true
To
Greater Than or
>= x >= y false
Equal To
JavaScript Variables
Variables in JavaScript behave the same as variables in most
popular programming languages (C, C++, etc) do, but in
JavaScript you don't have to declare variables before you use
them.
Using Variables
A variable's purpose is to store information so that it can be
used later. A variable is a symbolic name that represents
some data that you set.
When using a variable for the first time it is not necessary to use "var"
before the variable name, but it is a good programming practice to
make it clear when a variable is being used for the first time in
the program.
<html><body>
<script type="text/JavaScript">
var linebreak = "<br />"
var my_var = "Hello World!"
document.write(my_var)
document.write(linebreak)
document.write(linebreak)
my_var = "Script is Finishing up..."
document.write(my_var)
</script> </body>
</html>
JavaScript Functions
A function contains code that will be executed by an event
or by a call to the function.
Functions can be defined both in the <head> and in the
<body> section of a document. However, to assure that a
function is read/loaded by the browser before it is called, it
could be wise to put functions in the <head> section.
Syntax
function functionname(var1,var2,...,varX)
{
some code
}
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>
The return Statement
The return statement is used to specify the value that is returned from
the function.
<script type="text/javascript">
var x;
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
</script>
</body>
Event Handling in JavaScript
Events are actions that can be detected by JavaScript.
Examples of events:
• A mouse click
• A web page or an image loading
• Mousing over a hot spot on the web page
• Selecting an input field in an HTML form
• Submitting an HTML form
• A keystroke
For each events JavaScript provides a set of event handlers.
Button, Checkbox,
The object in question
FileUpload, Layer,
loses focus (e.g. by
onBlur Password, Radio, Reset,
clicking outside it or
Select, Submit, Text,
pressing the TAB key).
TextArea, Window
Button, Document,
onClick Checkbox, Link, Radio, The object is clicked on.
Reset, Submit
The object is double-
onDblClick Document, Link
clicked on.
An icon is dragged and
onDragDrop Window dropped into the
browser.
A JavaScript error
onError Image, Window
occurs.
Button, Checkbox,
The object in question
FileUpload, Layer,
gains focus (e.g. by
onFocus Password, Radio, Reset,
clicking on it or pressing
Select, Submit, Text,
the TAB key).
TextArea, Window
JavaScript Java
Compiled on server before
Interpreted by client.
execution on client.
Object-based. Code uses built-in,
Object-oriented. Applets consist
extensible objects, but no classes
of object classes with inheritance.
or inheritance.
Loose typing, variable data types Strong typing, variable data types
not declared. must be declared.