introducing java script

30

Upload: hassan-siddiqui

Post on 06-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 1/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 2/30

Recap of the last Lecture Java Script Basics

Java Script Introduction Java Script Statements Java Script Comments Java Script Variables

Java Script Operators Java Script Comparison Java Script Popup Boxes Recap of the Lecture

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 3/30

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 scriptsexecute without preliminary compilation)

Everyone can use JavaScript without purchasing a license

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 4/30

JavaScript gives HTML designers a programming tool

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/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 5/30

NO!

Java and JavaScript are two completely different languages

in both concept and design!

Java (developed by Sun Microsystems) is a powerful and

much more complex programming language - in the same

category as C and C++.

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 6/30

To insert a JavaScript into an HTML page, use the <script>

tag. Inside the <script> tag use the type attribute to define the

scripting language. The <script> and </script> tells where the JavaScript starts

and ends:

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 7/30

JS is a sequence of statements to be executed by the

browsers

JS is a case sensitive

A JavaScript statement is a command to a browser. The

purpose of the command is to tell the browser what to do.

This JavaScript statement tells the browser to write "HelloWorld" to the web page:

document.write (Hello World.) ;

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 8/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 9/30

Single Line Comments (//)

Multiple Line Comments (/* */) Comments are used to make code more readable

Comments may be used to prevent code execution

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 10/30

JavaScript variables are used to hold values or expressions

A variable can have a short name, like x, or a more

descriptive name, like carname.

Variable names are case sensitive (y and Y are two different

variables)

Variable names must begin with a letter or the underscorecharacter

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 11/30

Creating variables in JavaScript is most often referred to as

"declaring" variables.

You declare JavaScript variables with the var keyword

var x ; // Variable declaration

var carname ; // Variable declaration

x = 10; // Variable Initialization

carname = Volvo ; // Variable Initialization

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 12/30

A variable's value can change during the execution of a script. Youcan refer to a variable by its name to display or change its value.

1. <html> <body>

2. <script type="text/javascript">3. var lastname;4. lastname=Mujtaba";5. document.write(lastname);

6. document.write("<br />");7. lastname=Shaikh";8. document.write(lastname);9. </script>

10. </body> </html>

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 13/30

A variable declared within a JavaScript function becomesLOCAL and can only be accessed within that function. (thevariable has local scope).

You can have local variables with the same name in differentfunctions, because local variables are only recognized by the

function in which they are declared.

Local variables are destroyed when you exit the function.

You will learn more about functions in a later coming up

LABs.

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 14/30

Variables declared outside a function become GLOBAL, and

all scripts and functions on the web page can access it.

Global variables are destroyed when you close the page.

If you declare a variable, without using "var", the variable

always becomes GLOBAL.

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 15/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 16/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 17/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 18/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 19/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 20/30

In JavaScript we have the following conditional statements:

if statement - use this statement to execute some code only if aspecified condition is true

if...else statement - use this statement to execute some code if thecondition 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/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 21/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 22/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 23/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 24/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 25/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 26/30

Alert Box

Confirm Box

Prompt Box

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 27/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 28/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 29/30

8/3/2019 Introducing Java Script

http://slidepdf.com/reader/full/introducing-java-script 30/30

Java Script Basics

Java Script Introduction

Java Script Statements Java Script Comments

Java Script Variables

Java Script Operators

Java Script Comparison

Java Script Popup Boxes