240 likes | 410 Views
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.
E N D
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. • 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.
HOW TO ADD JAVA SCRIPT • This 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>
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)
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.
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;
MESSAGE BOX • Three pop-up windows that we can add in our web pages. • alertbox • confirmbox • promptbox
ALERTBOX • Used 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.
COMFIRMBOX • Used 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.
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.
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
IF SELECTION • Syntax: if (condition){action 1}; • Example: if (browser ==“Mozila Firefox”) { alert (“you are using Mozila Firefox”) };
IF/ELSE SELECTION • Syntax: if (condition){action 1} else {action 2}; • Example: if (browser ==“Mozila Firefox”) { alert (“you are using Mozila Firefox”) } else { alert (“you are using Netscape”) };
NESTED IF • 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”); } }; • Syntax: if (condition) { if (condition) {action 2} else {action 3} };
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; }
BOOLEAN OPERATOR • We 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.
LOOP • Used to make sure we can display data for more then one times. • 3 different loop: • for loop • while loop • do while loop
FOR LOOP & WHILE LOOP FOR 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 }
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);
FUNCTION • The 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.
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.
USER DEFINED FUNCTION • Syntax: 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.
ARRAY • An 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: • One Dimension • Two Dimension
ONE DIMENSIONAL ARRAY • Syntax: 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; };