chapter 10 java script

24

Upload: ekram

Post on 20-Jan-2016

61 views

Category:

Documents


0 download

DESCRIPTION

CHAPTER 10 JAVA SCRIPT. JAVA SCRIPT. Language that will allow you to add real programming to your web pages. Most of the browser today have their JavaScript interpreter. We used JavaScript for: Browser Detection-Detecting the browser used by a visitor at your page. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CHAPTER 10 JAVA SCRIPT
Page 2: CHAPTER 10 JAVA SCRIPT

JAVA SCRIPTLanguage that will allow you to add real programming to your web

pages.

Most of the browser today have their JavaScript interpreter.

We used JavaScript for:Browser Detection-Detecting the browser used by a visitor at your

page.Cookies- To store the information on the visitor computer and then

retrieve this information automatic next time the user visit your page.Control Browsers- Opening pages in customized windows, where you

specify if the browsers buttons, menu line, status line or whatever should be present.

Validate Forms-Validating inputs to fields before submitting a form.

Page 3: CHAPTER 10 JAVA SCRIPT

HOW TO ADD JAVA SCRIPTThis script tell the browser where JavaScript starts and ends.

Syntax : <Script language=“JavaScript”> </Script>

Example:<HTML><BODY><Script language =“JavaScript”> //type the message/text</Script></BODY></HTML>

Page 4: CHAPTER 10 JAVA SCRIPT

IMPORTANT !

A few things you need to know before you write a JavaScript code :

JavaScript-lines ends with a semicolon(;).Should always put the text within “ ”. If you forget to enclose your text in “ ”, JavaScript will

interpret your text as being variables rather than text.Capital letter are different from non-capital letters.(so you

need to follow the capital letters at exactly the right places)

Page 5: CHAPTER 10 JAVA SCRIPT

VARIABLES

All variables are referred to the unique name you assigned to them.

The variable with capital letter is difference with variable non-capital letters.

Example: Myname , MyName.

Page 6: CHAPTER 10 JAVA SCRIPT

ASSIGN VALUE TO VARIABLE Used equal-sign. We can assign a value or text to variables. Example:

Assigned a text to variables:FirstName=“Dit4”;

Assigned a value to variables:a= 3; a++;

Used a standard operator such as -, +, * and / in our web page. We also can used ++, -- and % operator for increment, decrement

and returns modulus value to a variable. Example:

a=2; b=a++;FirstName=“AIM”;LastName=“Alor Setar”;FullName=FirstName+ “ ”+LastName;

Page 7: CHAPTER 10 JAVA SCRIPT

MESSAGE BOXThree pop-up windows that we can add in our web pages.

alertboxconfirmboxpromptbox

Page 8: CHAPTER 10 JAVA SCRIPT

ALERTBOXUsed when we want to make sure an information comes

through to the user.

Syntax: alert (“text”);

Example: alert(“Welcome to my homepage”);

Message box windows will provide the “OK” button. So user need to click “OK” to proceed.

Page 9: CHAPTER 10 JAVA SCRIPT

COMFIRMBOXUsed when we want the user to agree to or accept something.

Syntax: confirm (“your text”);

Example: confirm (“You are 20 years old”);

Message box window will provide the “OK” and “ Cancel” button, so user need to click “OK” button or “CANCEL” button to proceed.

“OK” – returns the value true“CANCEL” – returns the value false.

Page 10: CHAPTER 10 JAVA SCRIPT

PROMPBOX Used when the user should input a value before entering the page.

Example: enter username, password.

Used together with variables.

Syntax: prompt (“your text”, “default value”);

Example: Username= prompt(“please enter your name”, “enter your name

here”);

User need to click “OK” button or “CANCEL” button after entering the text.

“OK” – the promptbox returns the entry. “CANCEL” – the promptbox returns null.

Page 11: CHAPTER 10 JAVA SCRIPT

SELECTION The selection control structure in JavaScript are:

If selection If/else selection Nested if selection Multiple selection (switch)

Comparing operator : = = equal to!= not equal to< less than> greater than< = less than or equal to> = greater than or equal to

Page 12: CHAPTER 10 JAVA SCRIPT

IF SELECTIONSyntax:

if (condition){action 1};

Example:if (browser ==“Mozila Firefox”){ alert (“you are using Mozila Firefox”)};

Page 13: CHAPTER 10 JAVA SCRIPT

IF/ELSE SELECTIONSyntax:

if (condition){action 1} else {action 2};

Example:if (browser ==“Mozila Firefox”){ alert (“you are using Mozila Firefox”)}else

{ alert (“you are using Netscape”)};

Page 14: CHAPTER 10 JAVA SCRIPT

NESTED IFSyntax:

if (condition){ if

(condition) {action 2} else {action 3}};

Example:

a= prompt(“please enter first number”, “0”);

b= prompt (“please enter second number”, “0”);

  if (a==b){ if (a>0) { Alert (“both number are equal

and a is positive number”); } };

Page 15: CHAPTER 10 JAVA SCRIPT

MULTIPLE SELECTION (SWITCH) Is used to handle decision making.

Syntax: switch (variablename){ case “label_1” : statements_1; break; case “label_2” : statements_2; break; case “label_n” : statement_n; break; default: statements;}

Page 16: CHAPTER 10 JAVA SCRIPT

BOOLEAN OPERATORWe call as a logical operator and use to enhance our if

statement.

Example of logical operator: AND (&&), OR (||) and NOT (!).

AND - Use when we want to check if more than one condition to be true.

OR- Use when we want to check if more than one condition should result in the check being true.

NOT- Use to invert whatever we are checking for.

Page 17: CHAPTER 10 JAVA SCRIPT

LOOP

Used to make sure we can display data for more then one times.

3 different loop: for loop while loop do while loop

Page 18: CHAPTER 10 JAVA SCRIPT

FOR LOOP & WHILE LOOPFOR LOOP Used when we know in advance how many times the script should

perform the similar task. Syntax:for (variable=startvalue; variable<=endvalue; variable= variable +

incrementfactot)

WHILE LOOP Used when we don’t know in advance how many times the loop

should be performed. Syntax:

while (variable <=endvalue){//type the JavaScript code,increment and decrement}

Page 19: CHAPTER 10 JAVA SCRIPT

DO WHILE LOOP Used when we don’t know in advance how many times the loop

should be performed.

Same with while loop, but in do.. While its test the condition at the end of the loop body.

The loop body always executed at least once.  Syntax

do { //type the JavaScript code; Increment/ decrement; } while (variable <=endvalue);

Page 20: CHAPTER 10 JAVA SCRIPT

FUNCTIONThe best way to develop and maintain a large program is to

construct it small, simple pieces or modules.

Modules in JavaScript are called functions.

JavaScript programs are written by combining new functions that the programmer writes with “prepackaged” functions and object available in JavaScript.

The “prepackaged” functions that belong to JavaScript objects (such as Math.pow and Math.round) are often called methods.

Page 21: CHAPTER 10 JAVA SCRIPT

JavaScript provides several objects that have a rich collection of methods for performing common mathematical calculations, string manipulations, date and time manipulations and manipulations of collections of data called Arrays.

The programmer can write functions to define specific tasks that may be used at many points in a script (referred as a programmer-defined functions).

A function is invoked by a function call.

The function call specifies the function name and provides information (as arguments) that the called function needs to do its task.

Page 22: CHAPTER 10 JAVA SCRIPT

USER DEFINED FUNCTIONSyntax:

function functionname (variable1,variable2….){//here goes the Javascript for the function}

Must be type after <script Language=…> Must be spelled exactly as function.To call the function, just called the functionname that you

give for your function.

 

Page 23: CHAPTER 10 JAVA SCRIPT

ARRAYAn array is a group of memory location that all have same

name and same type.

Array is used to help us to automate the process.

When using variable-array, we need to define the array before referring it to any of the variable.

2 types of array:o One Dimensiono Two Dimension

Page 24: CHAPTER 10 JAVA SCRIPT

ONE DIMENSIONAL ARRAYSyntax:

Variablename=new Array;

Example: Assign data to array by using looping:

var value=new Array;for (number=1; number<=100; number= number +

1){value[number]= number * 10;};