You are on page 1of 77

BSc IT

Web Designing
Semester - III

Copyright @ Amity University


Contents

• Introduction to Java Script


• Syntax and Flow Control
• Data Types
• Making Decisions
• Working with if Statements

Copyright @ Amity University


Contents
• Comparing Strings
• Loops
• for Loops
• Events And Events Handling

Copyright @ Amity University


Contents

• JavaScript Dialog Boxes


• Forms and Functions
• Introduction to XML
• XML Basics
• XML Structure
• Developing a DTD from XML code

Copyright @ Amity University


Introduction to Java Script
• JavaScript is an object-based scripting language

• Invented by Netscape in 1995.

•JavaScript is interpreted at the browser machine rather


than compiling on a specific machine.

•JavaScript is mainly based on the HTML

•JavaScript is most popular for writing scripts for client-side


applications over other script languages.

Copyright @ Amity University


Cont…

• Similar syntax to C, C++, and Java


• Interpreted, scripting language, not a full
programming language
• Platform-independent.
• Not really related to Java.
• Both client-side (browser-based)
JavaScript and server-side JavaScript.
Most focus is on the client.
Copyright @ Amity University
Cont…

• For Example JavaScript must be used in


creating applications like forms in which
you need to check whether the applicant
had entered valid data or not for further
processing of data.

Copyright @ Amity University


Java Script Applications

• We can create client-side forms with


validations of form’s fields
• We can create many interactive games
and multimedia applications
• We can create digital clocks which shows
client-side time rather server-side time.

Copyright @ Amity University


Starting Java Script

• To begin JavaScript we need

– Text Editor
– Browser
– To run JavaScript we need a compatible
browser like Netscape Navigator 2 .0 and up

Copyright @ Amity University


Object Oriented programming

• OOP is a programming technique


designed to simplify complicated
programming concepts.
• Object oriented programming revolves
around the objects.
• Object oriented programming consists of
objects ,Methods and Properties

Copyright @ Amity University


Objects ,Methods and Properties
• Objects
– An object is basically collection of properties
and methods with some characteristics with
which we can read, write ,modify and interact.
– E.g. Web page document is an object
– Each object has certain properties about the
object for e.g. the background color of
document object is written as document
.bgcolor
Copyright @ Amity University
Methods

• Methods are collection of actions that an


object can do.
• Different object performs different
activities.
• For e.g. We can open new document with
the help of a method called
document.open()
• We can pass and access the data through
these methods
Copyright @ Amity University
Functions

JavaScript instructions are usually grouped


together in a function:
• <script language=“javascript”>
• function myFunction(parameters) {
• // some logical grouping of code
• }
• </script>
• Like a method, procedure, or subroutine.
• Functions are called by events
Copyright @ Amity University
Example
• <html>
• <head>
• <script language=“javascript”>
• function funct() {
• // code
• }
• </script>
• </head>
• <body>
• <input type=button onClick=“funct()”>
• </body>
• </html>
Copyright @ Amity University
Events
• JavaScript is event-driven.
• Something has to happen before the
JavaScript is executed.
• JavaScript defines various events:
• onClick – link or image is clicked
• onSubmit – a form is submitted
• onMouseOver – the mouse cursor moves
over it
• onChange – a form control is changed
Copyright @ Amity University
• onLoad – something gets loaded in the
browser
• Events are specified in the HTML code.

Copyright @ Amity University


Events

• Many more events are:


– OnMouseClick
– OnMouseOver
– OnFocus
– OnBlur
– OnLoad
– OnUnload

Copyright @ Amity University


First program

• One way to put JavaScript in a page is


with the <SCRIPT> tag
• Start tag with <SCRIPT
LANGUAGE="javascript">
• End tag with </SCRIPT>
• Put JavaScript between the tags.

Copyright @ Amity University


Example

• <SCRIPT LANGUAGE="javascript">
document.write("Hello, world")
</SCRIPT>

Output:
• Hello, world

Copyright @ Amity University


Comment Statements

• Comment statements are used to prevent


the browser from executing certain parts of
code that you designate as non-code.
• The single line comment is just two
slashes (//) and the multiple line comment
starts with (/*) and ends with (*/). We will
talk about comments in greater depth in a
later lesson.
Copyright @ Amity University
Single Line comments

• <script type="text/javascript">
<!-- // This is a single line JavaScript
comment
document.write("I have comments in my
JavaScript code!");
//document.write("You can't see this!"); //-->
</script>

Copyright @ Amity University


Output

• I have comments in my JavaScript code!

Copyright @ Amity University


Multi- Line comments
• <script type="text/javascript">
• <!-- document.write("I have multi-line
comments!");
• /*document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");
document.write("You can't see this!");*/ //-->
</script>
Copyright @ Amity University
Output

• I have multi-line comments!

Copyright @ Amity University


JavaScript Data Types

• JavaScript has only three primitive


data types

strings : "foo" 'howdy do' "I


said 'hi'." ""
numbers : 12 3.14159 1.5E6
booleans : true false

Copyright @ Amity University


Example
• <html>
• <!-- Dave Reed js02.html 2/01/04 -->

• <head>
• <title>Data Types and Variables</title>
• </head>

• <body>
• <script type="text/javascript">
• x = 1024;
• document.write("<p>x = " + x + "</p>");

• x = “Peter";
• document.write("<p>x = " + x + "</p>");
• </script>
• </body>
• </html>
Copyright @ Amity University
Variables

• A variable is a named area in computer


memory that is used to store information
and from which information can be
retrieved. As JavaScript is a relatively
high-level language, it is not necessary to
know most of the details of how
information is stored in memory by a
JavaScript interpreter. JavaScript provides
for the automatic creation of variables
whenever values are assigned to them.
Copyright @ Amity University
Cont…

• Variables are declared with the var


keyword:
• var num = “1”;
• var name = “Mel”;
• var phone = “123-456-7890”;

Copyright @ Amity University


Types of creating variables

• var x1 = 'He said "be seeing you" as he


left.'
• document.write(x1)
• x = "New Message“
• document.write(x1)

Copyright @ Amity University


Example

<script language=“JavaScript”>

<!-- definition of variables


var num_car= 25;
var passenger_per_car= 3;

//calculation of total number of people


var total_passenger= num_car * passenger_per_car

Alert(total_passenger);

// end of script -->

</script>

Copyright @ Amity University


alert(), confirm(), and prompt()
functions
Alert

A JavaScript alert is a little window


that contains some message:
alert(“This is an alert!”);
• Are generally used for warnings.

Copyright @ Amity University


Example
• <html>
• <head>
• <script language=“javascript”>
• function showAlert(text) {
• alert(text);
• }
• </script>
• </head>
• <body onload=“showAlert(„This alert displays when
• the page is loaded!‟);”>

Copyright @ Amity University


Prompt

• A prompt box is used when user wants to


input a value before entering a page.
• When a prompt box pops up, the user will
have to click either "OK" or "Cancel" to
proceed after entering an input value.
• If the user clicks "OK" the box returns the
input value. If the user clicks "Cancel" the
box returns null.
Copyright @ Amity University
Syntax
• prompt("sometext","defaultvalue");

Copyright @ Amity University


Example
• <html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
{
document.write("Hello " + name + "! How are you today?");
}
}
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="Show prompt
box" />
</body>
</html>

Copyright @ Amity University


Confirm Box

• A confirm box is often used if you want the


user to verify or accept something.
• When a confirm box pops up, the user will
have to click either "OK" or "Cancel" to
proceed.
• If the user clicks "OK", the box returns
true. If the user clicks "Cancel", the box
returns false
Copyright @ Amity University
Syntax

• confirm("sometext");

Copyright @ Amity University


Confirm
• html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
</script>
</head>
<body>
<input type="button" onclick="show_confirm()" value="Show confirm box" />
</body>
</html>
Copyright @ Amity University
Operators and expressions

– +, -, /, % (arithmetic)

– >, <, >=, lt=, ==, != (comparisons)

– &&, ||, ! (boolean)

Copyright @ Amity University


Cont..

• == the numbers or objects or values must


be equal
• ! Logical NOT
• || Logical OR
• != the numbers or objects or values must
not be equal
• >= number on the right must be less than
or equal to the number on the left
• && Logical AND
Copyright @ Amity University
Cont..

• < number on the right must be greater


than or equal to the number on the left
• <=
• < number on the right must be greater
than the number on the left
• > number on the left must be greater than
the number on the right

Copyright @ Amity University


Cont..

• = assigns the value on the right to the


object on the left
• the object on the left = the object on the
left + the value on the right
• this also works when appending strings
• +=
• -= the object on the left = the object on the
left - the value on the right
Copyright @ Amity University
Cont..

• % Modulus - divide the first number by the


second and return the remainder
• * multiplies two numbers
• ++ / -- Increment / Decrement a number

Copyright @ Amity University


Cont..

• / divides the first number by the second


• - subtracts the second number from the
first
• adds two numbers or appends two strings.
If more than one type of
• variable is appended, including a string
appended to a number or viceversa,
• the result will be a string
Copyright @ Amity University
If Statement

• To make use of conditional statements


that allow program to make decisions
• The "If Statement" is a way to make
decisions based on a variable or some
other type of data.

Copyright @ Amity University


If Statement

• In JavaScript we have the following


conditional statements:

Syntax

• if (condition)
{
code to be executed if condition is true
}
Copyright @ Amity University
If Statement

• if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not
true
}
Copyright @ Amity University
if-else statement

var rightAnswer = 57;


var num = prompt("Please guess a number
below 100: ","");
if (num < rightAnswer)
{
alert("Your guess was too low");
}

Copyright @ Amity University


Cont..
else
if (num > rightAnswer)
{
alert("Your guess was too high");
}
else
{
alert("Congratulations!");
}
Copyright @ Amity University
else
{
alert("Too bad!");
}

Copyright @ Amity University


Example
• <script type="text/javascript">
var red = 5;
var blue = 3;
var match = null;
if (red == blue) {
match = 'equal';
} else {
match = 'unequal';
}
document.write(red + ' and ' + blue + ' are ' +
match);
</script>
Copyright @ Amity University
output

• 5 and 3 are unequal

Copyright @ Amity University


JavaScript Switch Statement

• switch statement is used to select one of


many blocks of code to be executed.
• Multiway branch

• The switch value does not need to a


number. It can be a string.

• The case values can be expressions.


Copyright @ Amity University
JavaScript Switch Statement
Syntax
switch (expression) {
case ';':
case ',':
case '.':
punctuation();
break;
default:
noneOfTheAbove();
}
Copyright @ Amity University
JavaScript Switch Example
• <html>
• <head>
• <script type="text/javascript">
• <!--
• var myColor = "Red";

• switch (myColor)
• {
• case "Blue":
• document.write("Just like the sky!");
• break
• case "Red":
• document.write("Just like shiraz!");
• break
• default:
• document.write("Suit yourself then...");
• }
• //-->
• </script>

• </head>

• </html>

Copyright @ Amity University


Output
• While loop is beginning
myCounter = 0
myCounter = 1
myCounter = 2
myCounter = 3
myCounter = 4
myCounter = 5
myCounter = 6
myCounter = 7
myCounter = 8
myCounter = 9
While loop is finished!

Copyright @ Amity University


for

• Iterate through all of the elements of


an array:

for (var i = 0; i < array.length; i += 1)


{

Copyright @ Amity University


for loop

var MAX = 10;


var count;
for (count = MAX; count >= 1; count--)
{
document.write("Countdown: " + count +
"<BR>");
}

Copyright @ Amity University


Output
• Countdown: 10
Countdown: 9
Countdown: 8
Countdown: 7
Countdown: 6
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1

Copyright @ Amity University


Examples Using Events
onMouseOver() event:
• <html>
• <head>
<SCRIPT LANGUAGE="javascript">
function oops()
{
alert("You got too close!")
}
</SCRIPT>
</head>
<body>
<Input type=button value=Move mouse over this for a test
onMouseOver="oops()">
</body>
</html>

Copyright @ Amity University


OnClick
<html>
<head>
<script type="text/javascript">
<!-- function popup()
{
alert("Hello World") } //-->
</script>
</head>
<body>
<input type="button" value="Click Me!" onclick="popup()"><br />
</body>
</html>

Copyright @ Amity University


on Change() event:
• <html>
• <head>
• <script type="javascript">
• </script>
• </head>
• <body>
• <FORM name=myform>
• From Field <INPUT TYPE="text" NAME="text1"
onChange="document.myform.text2.value =

• document.myform.text1.value"><BR>
• To Field <INPUT TYPE="text" NAME="text2">
• </FORM>

• </body>
• </html>
Copyright @ Amity University
Output
• From Field
• To Field

Copyright @ Amity University


• Please enter your name: <input type="text"
name="your_name" onchange="validateField(this)"/>
<script type="text/javascript"> function validateField (
fieldname ) { if ( ( fieldname.value ) && ( !
fieldname.value.match ( " " ) ) ) { alert ( "Please enter
your first and last names!" ); fieldname.focus ( ); } }
</script>

Copyright @ Amity University


output

• Please enter your name:

After entering Name it will display alert box


showing the message

“please enter your first and last name

Copyright @ Amity University


onFocus

• onFocus is executed whenever the


specified object gains focus. This usually
happens when the user clicks on the
object with the mouse button, or moves to
the object using the TAB key. onFocus
can be used on most form elements.

Copyright @ Amity University


Example
• <html>
• <head>
• <script type="text/javascript">

• </script>
• </head>
• <body>

• <form>
• <form>
• <input type="text" name="email_address"
• size="40" value="Please enter your email address"
• onfocus="this.value=''"/>
• </form>
• </body>

• </html>

Copyright @ Amity University


Output

Please enter your email address

Copyright @ Amity University


onLoad

• The onLoad event handler is triggered


when the page has finished loading.
Common uses of onLoad include the
dreaded pop-up advertisement windows,
and to start other actions such as
animations or timers once the whole page
has finished loading.
• Example

Copyright @ Amity University


Example
• <html>
• <head>
• <script type="text/javascript">

• </script>
• </head>
• <body onload = "alert('Thanks for visiting my page!')">

• My Page

• </body>
• </html>

Copyright @ Amity University


Output

• My Page

Thanks for visiting my site

Copyright @ Amity University


Arrays

var score = new Array(3);

score[0] = 35
score[1] = 56
score[2] = 10

Alternative : var score = new Array(35,56,10);

sum=score[0]+score[1]+score[2];

alert(sum) ;
Copyright @ Amity University
Example
• <html>
• <HEAD>
• <SCRIPT language="JavaScript">
• <!--
• function display_quote()
• {
• var quote= new Array(5)
• quote[0]="I like JavaScript.";
• quote[1]="I used to like Java.";
• quote[2]="JavaScript rules.";
• quote[3]="Help! JavaScript Error!";
• quote[4]="Just Kidding.";
• var x=0;
• for (x=0; x<5; x++)
• {
• alert(quote[x]);
• }
• }
• //-->
• </SCRIPT>
• </HEAD>
• <BODY>
• <input type=button onClick="display_quote()">Click Here, I dare you!
• </BODY>
• </html>

Copyright @ Amity University


Function

• 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.

Copyright @ Amity University


Syntax

• function functionname(var1,var2,...,varX)
{
some code
}
• The parameters var1, var2, etc. are
variables or values passed into the
function. The { and the } defines the start
and end of the function.

Copyright @ Amity University


Example

• <html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>
<body>
<input type="button" value="Click me!"
onclick="displaymessage()" />

</body>
</html>

Copyright @ Amity University


Thank You

Copyright @ Amity University

You might also like