Web Engineering
Hassan Khan
JavaScript
JavaScript is the scripting language of the
Web.
JavaScript is used in millions of Web pages
to add functionality, validate forms, detect
browsers, and much more.
8/9/2010 Hassan Khan 2
What is JavaScript?
JavaScript was designed to add interactivity to HTML
pages
JavaScript is a scripting language
A scripting language is a lightweight programming
language
JavaScript is usually embedded directly into HTML
pages
JavaScript is an interpreted language (means that
scripts execute without preliminary compilation)
Everyone can use JavaScript without purchasing a
license
8/9/2010 Hassan Khan 3
Example
<html>
<body>
<script type="text/javascript">
document.write("This is my first JavaScript!");
</script>
</body>
</html>
8/9/2010 Hassan Khan 4
How to Handle Simple
Browsers
Browsers that do not support JavaScript, will display
JavaScript as page content.
The HTML comment tag should be used to "hide" the
JavaScript.
Just add an HTML comment tag <!-- before the first
JavaScript statement, and a --> (end of comment) after
the last JavaScript statement.
8/9/2010 Hassan Khan 5
Example
The two forward slashes at the end of comment line (//) is
the JavaScript comment symbol. This prevents
JavaScript from executing the --> tag.
8/9/2010 Hassan Khan 6
What can a JavaScript do?
JavaScript gives HTML designers a programming
tool
JavaScript can put dynamic text into an HTML page
JavaScript can react to events
JavaScript can read and write HTML elements
JavaScript can be used to validate data
JavaScript can be used to detect the visitor's
browser
JavaScript can be used to create cookies
8/9/2010 Hassan Khan 7
How To Use?
<script> tag is used to insert a JavaScript into
an HTML page.
Between <body> tag
Between <head> tag
8/9/2010 Hassan Khan 8
How To Use (Cont.)?
JavaScripts in the body section will be
executed WHILE the page loads.
JavaScripts in the head section will be
executed when CALLED.
8/9/2010 Hassan Khan 9
JavaScript Statements
JavaScript is case sensitive.
Use of semicolon(;) in the end of statement is
optional.
document.write("Hello");
document.write("Hello")
8/9/2010 Hassan Khan 10
JavaScript Comments
Single line comments start with //.
Multi line comments start with /* and end with
*/.
8/9/2010 Hassan Khan 11
JavaScript Variables
JavaScript variables are used to hold values
or expressions.
x=5, y=6, z=x+y
Rules for JavaScript variable names:
Variable names are case sensitive (y and Y
are two different variables)
Variable names must begin with a letter or
the underscore character
8/9/2010 Hassan Khan 12
JavaScript Variables
Declaration
var x;
var carname;
Assign Values
x=5;
carname=“Toyota";
8/9/2010 Hassan Khan 13
JavaScript Arithmetic Operators
Y=5
8/9/2010 Hassan Khan 14
JavaScript Assignment Operators
x = 5 and y=10
8/9/2010 Hassan Khan 15
+ Operator and Strings
To add two or more string variables together,
use the + operator.
8/9/2010 Hassan Khan 16
Comparison Operators
8/9/2010 Hassan Khan 17
Logical Operators
8/9/2010 Hassan Khan 18
If...Else Statements
used to perform different actions based on different
conditions.
if statement - use this statement to execute some
code only if a specified condition is true
if...else statement - use this statement to execute
some code if the condition is true and another code
if the condition is false
if...else if....else statement - use this statement to
select one of many blocks of code to be executed
switch statement - use this statement to select one
of many blocks of code to be executed
8/9/2010 Hassan Khan 19
Example (If)
8/9/2010 Hassan Khan 20
Example (If...else)
8/9/2010 Hassan Khan 21
Example(If...else if...else)
8/9/2010 Hassan Khan 22