PK-WT-UNIT - 04 - JavaScript Introduction
PK-WT-UNIT - 04 - JavaScript Introduction
Web Technologies
welcome4.html
10
11 <script type = "text/javascript">
12 <!--
alert method of the window
1 of 1
13 window.alert( "Welcome to\nJavaScript\nProgramming!" );
14
object displays a Dialog box
// -->
15 </script>
16
17 </head>
18
19 <body>
20 <p>Click Refresh (or Reload) to run this script again.</p>
21 </body>
22 </html>
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<script language="JavaScript">
document.write('This is my first
JavaScript Page');
document.write('<h1>This is my first
JavaScript Page</h1>');
<form>
<input type="button" Value="Press"
onClick="window.alert('Hello');">
</form>
HTML Forms and JavaScript
• JavaScript is very good at processing user
input in the web browser
• HTML <form> elements receive input
• Forms and form elements have unique names
– Each unique element can be identified
– Uses JavaScript Document Object Model (DOM)
Naming Form Elements in HTML
<form name="addressform">
Name: <input name="yourname"><br />
Phone: <input name="phone"><br />
Email: <input name="email"><br />
</form>
Forms and JavaScript
document.formname.elementname.value
Thus:
document.addressform.yourname.value
document.addressform.phone.value
document.addressform.email.value
Using Form Data
Personalising an alert box
<form name="alertform">
Enter your name:
<input type="text" name="yourname">
<input type="button" value= "Go"
onClick="window.alert('Hello ' +
document.alertform.yourname.value);">
</form>
JavaScript Statements
• JavaScript statements are "commands" to the
browser.
• The purpose of the statements is to tell the
browser what to do.
• Semicolon ;
– Semicolon separates JavaScript statements.
– Normally we add a semicolon at the end of each
executable statement.
– Using semicolons also makes it possible to write many
statements on one line.
• JavaScript variables
– are "containers" for storing information:
– Example
var x=5;
var y=6;
var z=x+y;
– Example
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
– Extra large or extra small numbers can be written
with scientific (exponential) notation:
– Example
var y = 123e5; // 12300000
var z = 123e-5; // 0.00123
• JavaScript Strings
– A string is a variable which stores a series of
characters like "John Doe".
– A string can be any text inside quotes. You can use
single or double quotes:
– Example
var carname="Volvo XC60";
var carname='Volvo XC60';
– You can use quotes inside a string, as long as they
don't match the quotes surrounding the string:
– Example
var answer="It's alright";
var answer="He is called 'Johnny'";
var answer= “He is called "Johnny”
• JavaScript Booleans
– Booleans can only have two values: true or false.
– Example
var x = true;
var y = false;
– Booleans are often used in conditional testing.
• JavaScript Arrays
– JavaScript arrays are written with square brackets.
– Array items are separated by commas.
– The following code declares (creates) an array
called cars, containing three items (car names):
– Example
var cars = ["Saab", "Volvo", "BMW"];
– Array indexes are zero-based, which means the
first item is [0], second is [1], and so on.
• JavaScript Objects
– JavaScript objects are written with curly braces.
– Object properties are written as name:value pairs,
separated by commas.
– Example
var person = {firstName:"John", lastName:"Doe", age:50,
eyeColor:"blue"};
Subtraction - p–c p - c
* x-- b * m
Multiplication bm y
Division / x / y or or x y x / y
Remainder % r mod s r % s
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
>= x >= y x is greater than or
equal to y
<= x <= y x is less than or equal to
£ y
Precedence and Associativity of the operators
\' Single quote. Used to represent a single quote character in a string. For
example,
window.alert( '\'in quotes\'' );
displays 'in quotes' in an alert dialog.