Java script

41
JavaScript

Transcript of Java script

section of the HTML page.Scripts In BodyIn Head Scripts added to the section are to be executed when called.Scripts added to the section are to be executed when the page loads. Scripts are better to be included at the end of the HTML page inorder to prevent slow loading of pages. 4. JavaScript Syntax JavaScript statements ends with ; JavaScript is case sensitive JavaScript comments are like C++ and Java comments// for single line comments and /* */ for multi-line comments 5. JavaScript Output Writing to The HTML Document Output: To write HTML element using JavaScript we user document.write("HTML Element"); Example To write a paragraph using JavaScript: 6. JavaScript Output (Cont.) NOTE:If you execute document.write after the document has finished loading, the entire HTML page will be overwritten. Example:

My Paragraph.

Try it 7. JavaScript Output (Cont.) 8. JavaScript Variables Variable names must begin with a letter or $ or _. JavaScript is a weakly-typed language, meaning that variables aredeclared without specifying data-types. You can declare variables with the var keyword. Examples: var x = 1; var y = 1.2; var str = "Hello World"; var check = "true"; var cars=new Array("Saab", "Volvo", "BMW"); Note: If you assign a value to variable that has not yet been declared, the variable will automatically be declared as a GLOBALvariable. 9. JavaScript Objects To Create Object and add properties to it: 1. var person = { firstname : "John", lastname : Adam", age : 50 }; 2. Var person = new Object(); person.firstname="John"; person.lastname=Adam"; person.age=50; To Access Object properties: 1. 2.name=person.lastname; name=person["lastname"]; 10. JavaScript Functions Syntax:function functionname(argument1, argument2, ) { // some code to be executed return value; } To call: var returnVar = functionname(argument1, argument2, ); 11. JavaScript Operators JavaScript arithmetic operators (like C++ and Java): + / * ++ -% JavaScript assignment operators (like C++ and Java): = += -= /= JavaScript logical operators (like C++ and Java): && || ! JavaScript string concatenation operator: + JavaScript comparison operators : != == > < = === !== 12. JavaScript Conditions If...else if...else Statement: Syntax: if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if neither condition1 nor condition2 is true } 13. JavaScript Conditions (Cont.) Switch Statement: Syntax: switch(n) { case 1: // execute code block 1 break; case 2: // execute code block 2 break; default: // code to be executed if n is different from case 1 and 2 } 14. JavaScript Loops For Loop: Syntax: for (statement 1; statement 2; statement 3) { // code block to be executed } For .. In Loop: Syntax: for (variable in object) { // code to be executed } Example: var person={fname:"John", lname:Adam", age:25}; for (x in person) { var personElement = person[x]; } 15. JavaScript Loops (cont.) While Loop: Syntax: while (condition) { // code block to be executed } Do .. While Loop: Syntax: do { // code block to be executed } while (condition); 16. JavaScript Break and Continue Break Statement: It breaks the loop and continues executing the code after the loop (if any). Syntax: break; Continue Statement: It breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop. Syntax: continue; 17. JavaScript Strings String Length: You can get the length of a string using length property. Example: var txt="Hello World"; document.write(txt.length); 11 Finding a String in a String: You can find a string in another string using either indexOf() or lastIndexOf() method. The method returns -1 if the specified text is not found. Example: var str="Hello world"; var n = str.indexOf("l"); 2 var m = str.lastIndexOf("l"); 9 18. JavaScript Strings (Cont.) Matching Content: You can search for a matching content in a string using match() method. This method returns the matching content if present, else it returns null. Example: var str="Hello World"; document.write(str.match("world")); null document.write(str.match("World")); World Replacing Content: You can replace a specified value with another value in a string using replace() method. Example: var str=Hello World" var n=str.replace(World",FCI"); Hello FCI 19. JavaScript Strings (Cont.) Upper Case and Lower Case: You can convert a string to upper/lower case with the methods toUpperCase() / toLowerCase(). Example: var txt="Hello World"; var txt1=txt.toUpperCase(); HELLO WORLD var txt2=txt.toLowerCase(); hello world Convert a String to an Array: You can convert a string to an array with split() method. Example: var str="a,b,c,d,e,f; var arr=str.split(","); array {a, b, c, d, e, f} 20. JavaScript Strings (Cont.) Take part from a String: You can take part from a string using substring() method. Example: var str="Hello World"; var substr=str.substring(2, 5); llo Get Character from a String: You can get a char from a string using charAt() method. Example: var str="Hello World"; var c=str.charAt(1); e 21. JavaScript Strings (Cont.) Concatenate two Strings: You can concatenate two strings using concat() method. Example: var txt1="Hello"; var txt2="World"; var txt = txt1.concat(txt2); HelloWorld 22. JavaScript Date Initiating a date: 1. new Date(); // current date and time 2. new Date(dateString); 3. new, Date(year month, day, hours, minutes, seconds); Example: var n = new Date(79,5,24,11,33,o); document.write(n); Result: Sun Jun 24 1979 11:33:00 GMT+0200 (Egypt Standard Time)Example: var n = new Date("June 24, 1979 11:33:00"); document.write(n); Result: Sun Jun 24 1979 11:33:00 GMT+0200 (Egypt Standard Time) 23. JavaScript Date (Cont.) Set Date: 1. setFullYear(year, month, day): Example: var myDate=new Date(); myDate.setFullYear(2010,0,14); 1. setDate(dateobject): Example: var myDate=new Date(); myDate.setDate(myDate.getDate()+5); Compare Two Dates: To compare dates use >,