Java script final presentation


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java script final presentation

  1. 1. <ul><li>Building Web Sites: Introduction to JavaScript </li></ul>Kaushal Kishore Software Engineer OSSCube LLC [email_address]
  2. 2. What is JavaScript <ul><ul><li>JavaScript was originally developed by Brendan Eich of Netscape under the name  Mocha , which was later renamed to  LiveScript , and finally to JavaScript </li></ul></ul><ul><ul><li>A lightweight programming language that runs in a Web browser </li></ul></ul><ul><ul><li>JavaScript is a Client Side Scripting Language. </li></ul></ul><ul><ul><li>Also known as ECMAScript  </li></ul></ul><ul><ul><li>Interpreted, not compiled. </li></ul></ul><ul><ul><li>JavaScript Syntax are similar to C and Java Language. </li></ul></ul><ul><ul><li>JavaScript code is usually embedded directly into HTML pages </li></ul></ul><ul><ul><li>JavaScript is reasonably platform-independent </li></ul></ul>
  3. 3. What Can JavaScript Do? <ul><li>JavaScript gives HTML designers a programming tool </li></ul><ul><li>JavaScript can put dynamic text into an HTML page </li></ul><ul><li>JavaScript can react to events </li></ul><ul><li>JavaScript can read and write HTML elements </li></ul><ul><li>JavaScript can be used to validate input data </li></ul><ul><li>JavaScript can be used to detect the visitor's browser </li></ul><ul><li>JavaScript can be used to create cookies </li></ul>
  4. 4. When not to use JavaScript <ul><li>When you need to access other resources. </li></ul><ul><ul><li>Files Programs </li></ul></ul><ul><ul><li>Databases </li></ul></ul><ul><li>When you are using sensitive or copyrighted data or algorithms. </li></ul><ul><ul><li>Your JavaScript code is open to the public . </li></ul></ul>
  5. 5. JavaScript is not Java <ul><li>Java and JavaScript are two completely different languages in both concept and design! </li></ul><ul><li>JavaScript has some features that resemble features in Java: </li></ul><ul><ul><li>JavaScript has Objects and primitive data types </li></ul></ul><ul><ul><li>JavaScript has qualified names; for example, document.write(&quot;Hello World&quot;); </li></ul></ul><ul><ul><li>JavaScript has Events and event handlers </li></ul></ul><ul><ul><li>Exception handling in JavaScript is almost the same as in Java </li></ul></ul><ul><li>JavaScript has some features unlike anything in Java: </li></ul><ul><ul><li>Variable names are untyped: the type of a variable depends on the value it is currently holding </li></ul></ul><ul><ul><li>Objects and arrays are defined in quite a different way </li></ul></ul><ul><ul><li>JavaScript is an interpreted language but java is both interpreted and compiled </li></ul></ul>
  6. 6. Where Do You Place Scripts? <ul><li>JavaScript can be put in the <head> or in the <body> of an HTML document </li></ul><ul><ul><li>JavaScript functions should be defined in the <head> </li></ul></ul><ul><ul><ul><li>This ensures that the function is loaded before it is needed </li></ul></ul></ul><ul><ul><li>JavaScript in the <body> will be executed as the page loads </li></ul></ul><ul><li>JavaScript can be put in a separate .js file </li></ul><ul><ul><li><script src=&quot;myJavaScriptFile.js&quot;></script> </li></ul></ul><ul><ul><li>Put this HTML wherever you would put the actual JavaScript code </li></ul></ul><ul><ul><li>An external .js file lets you use the same JavaScript on multiple HTML pages </li></ul></ul><ul><ul><li>The external .js file cannot itself contain a <script> tag </li></ul></ul><ul><li>JavaScript can be put in HTML form object, such as a button </li></ul><ul><ul><li>This JavaScript will be executed when the form object is used </li></ul></ul>
  7. 7. Referencing External JavaScript File <ul><li>Scripts can be provided locally or remotely accessible JavaScript file using src attribute </li></ul><ul><ul><ul><li><html> </li></ul></ul></ul><ul><ul><ul><li><head> </li></ul></ul></ul><ul><ul><ul><li><script language=&quot;JavaScript“ type=&quot;text/javascript“ src=&quot;http://somesite/myOwnJavaScript.js&quot;> </li></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul><ul><ul><ul><li><script language=&quot;JavaScript“ type=&quot;text/javascript“ src=&quot;myOwnSubdirectory/myOwn2ndJavaScript.js&quot;> </li></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul>
  8. 8. Primitive Datatypes <ul><li>JavaScript has three “primitive” types: number , string , and boolean </li></ul><ul><li>Numbers are always stored as floating-point values </li></ul><ul><ul><li>Hexadecimal numbers begin with 0x </li></ul></ul><ul><ul><li>Some platforms treat 0123 as octal, others treat it as decimal </li></ul></ul><ul><li>Strings may be enclosed in single quotes or double quotes </li></ul><ul><ul><li>Strings can contains n (newline), &quot; (double quote), etc. </li></ul></ul><ul><li>Booleans are either true or false </li></ul><ul><ul><li>0 , &quot;0&quot;, empty strings, undefined , null , and NaN are false , other values are true </li></ul></ul>
  9. 9. JavaScript Variable <ul><li>You create a variable with or without the “var” statement </li></ul><ul><ul><ul><li>var num = “1”; </li></ul></ul></ul><ul><ul><ul><li>var name = “Kaushal”; </li></ul></ul></ul><ul><ul><ul><li>var phone = “123-456-7890”; </li></ul></ul></ul><ul><li>Variables names must begin with a letter or underscore </li></ul><ul><li>Variable names are case-sensitive </li></ul><ul><li>Variables are untyped (they can hold values of any type) </li></ul><ul><li>The word var is optional (but it’s good style to use it) </li></ul><ul><li>Variables declared within a function are local to that function (accessible only within that function) </li></ul><ul><li>Variables declared outside a function are global (accessible from anywhere on the page) </li></ul>
  10. 10. Comments <ul><li>Comments are as in C or Java: </li></ul><ul><ul><li>Single Line Comment // </li></ul></ul><ul><ul><li>Paragraph Comment /*….*/ </li></ul></ul><ul><li>Java’s javadoc comments, /** ... */, are treated just the same as /* ... */ comments ; they have no special meaning in JavaScript </li></ul>
  11. 11. Operators of JavaScript 1 <ul><li>Because most JavaScript syntax is borrowed from C (and is therefore just like Java), we won’t spend much time on it </li></ul><ul><li>Arithmetic operators: + - * / % ++ -- </li></ul><ul><li>Comparison operators: < <= == != >= > </li></ul><ul><li>Logical operators: && || ! (&& and || are short-circuit operators) </li></ul><ul><li>Bitwise operators: & | ^ ~ << >> >>> </li></ul><ul><li>Assignment operators: += -= *= /= %= <<= >>= >>>= &= ^= |= </li></ul>
  12. 12. Operators of JavaScript 2 <ul><li>String operator: + </li></ul><ul><li>The conditional operator: condition ? value_if_true : value_if_false </li></ul><ul><li>Special equality tests: </li></ul><ul><ul><li>== and != try to convert their operands to the same type before performing the test </li></ul></ul><ul><ul><li>=== and !== consider their operands unequal if they are of different types </li></ul></ul><ul><li>Additional operators (to be discussed): new typeof void delete </li></ul>
  13. 13. JavaScript Conditional Statements <ul><li>In JavaScript we have the following conditional statements: </li></ul><ul><ul><li>if statement </li></ul></ul><ul><ul><li>if...else statement </li></ul></ul><ul><ul><li>if...else if....else statement </li></ul></ul><ul><ul><li>s witch statement </li></ul></ul>
  14. 14. JavaScript Looping Statement <ul><li>while  </li></ul><ul><ul><ul><li>Syntax: </li></ul></ul></ul><ul><ul><ul><li>while ( condition ) { code to be executed } </li></ul></ul></ul><ul><li>d o...while </li></ul><ul><ul><ul><li>Syntax: </li></ul></ul></ul><ul><ul><ul><li>do { code to be executed } while ( condition ) </li></ul></ul></ul><ul><li>For </li></ul><ul><ul><ul><li>Syntax: </li></ul></ul></ul><ul><ul><ul><ul><li>for ( initialization ; condition ; increment ) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>{ code to be executed } </li></ul></ul></ul></ul><ul><li>For…in </li></ul><ul><ul><ul><li>Syntax: </li></ul></ul></ul><ul><ul><ul><li>for ( variable  in  object ) </li></ul></ul></ul><ul><ul><ul><li>{ code to be executed } </li></ul></ul></ul>
  15. 15. Example of Statement <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><script> </li></ul><ul><li>var person={fname:&quot;John&quot;,lname:&quot;Doe&quot;,age:25}; </li></ul><ul><ul><li>for (x in person) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>document.write(person[x] + &quot; &quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li></script> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  16. 16. Array Literals <ul><li>You don’t declare the types of variables in JavaScript </li></ul><ul><li>JavaScript has array literals, written with brackets and commas </li></ul><ul><ul><li>Example: color = [&quot;red&quot;, &quot;yellow&quot;, &quot;green&quot;, &quot;blue&quot;]; </li></ul></ul><ul><ul><li>Arrays are zero-based: color[0] is &quot;red&quot; </li></ul></ul><ul><li>If you put two commas in a row, the array has an “empty” element in that location </li></ul><ul><ul><li>Example: color = [&quot;red&quot;, , , &quot;green&quot;, &quot;blue&quot;]; </li></ul></ul><ul><ul><ul><li>color has 5 elements </li></ul></ul></ul><ul><ul><li>However, a single comma at the end is ignored </li></ul></ul><ul><ul><ul><li>Example: color = [&quot;red&quot;, , , &quot;green&quot;, &quot;blue”,]; still has 5 elements </li></ul></ul></ul>
  17. 17. Four ways to create an Array <ul><li>You can use an array literal: var colors = [&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;]; </li></ul><ul><li>You can use new Array() to create an empty array: </li></ul><ul><ul><ul><li>var colors = new Array(); </li></ul></ul></ul><ul><ul><ul><li>You can add elements to the array later: </li></ul></ul></ul><ul><ul><ul><li>colors[0] = &quot;red&quot;; colors[2] = &quot;blue&quot;; colors[1]=&quot;green&quot;; </li></ul></ul></ul><ul><li>You can use new Array(n) with a single numeric argument to create an array of that size </li></ul><ul><ul><li>var colors = new Array(3); </li></ul></ul><ul><li>You can use new Array(…) with two or more arguments to create an array containing those values: </li></ul><ul><ul><li>var colors = new Array(&quot;red&quot;,&quot;green&quot;, &quot;blue&quot;); </li></ul></ul>
  18. 18. The length of an array <ul><li>If myArray is an array, its length is given by myArray.length </li></ul><ul><li>Array length can be changed by assignment beyond the current length </li></ul><ul><ul><li>Example: var myArray = new Array(5); myArray[10] = 3; </li></ul></ul><ul><li>Arrays are sparse, that is, space is only allocated for elements that have been assigned a value </li></ul><ul><ul><li>Example: myArray[50000] = 3; is perfectly OK </li></ul></ul><ul><ul><li>But indices must be between 0 and 2 32 -1 </li></ul></ul><ul><li>As in C and Java, there are no two-dimensional arrays; but you can have an array of arrays: myArray[5][3] </li></ul>
  19. 19. Arrays and objects <ul><li>Arrays are objects </li></ul><ul><li>car = { myCar: &quot;Saturn&quot;, 7: &quot;Mazda&quot; } </li></ul><ul><ul><li>car[7] is the same as car.7 </li></ul></ul><ul><ul><li>car.myCar is the same as car[&quot;myCar&quot;] </li></ul></ul><ul><li>If you know the name of a property, you can use dot notation: car.myCar </li></ul><ul><li>If you don’t know the name of a property, but you have it in a variable (or can compute it), you must use array notation: car.[&quot;my&quot; + &quot;Car&quot;] </li></ul>
  20. 20. Array functions <ul><li>If myArray is an array, </li></ul><ul><ul><li>myArray.sort() sorts the array alphabetically </li></ul></ul><ul><ul><li>myArray.reverse() reverses the array elements </li></ul></ul><ul><ul><li>myArray.push(…) adds any number of new elements to the end of the array, and increases the array’s length </li></ul></ul><ul><ul><li>myArray.pop() removes and returns the last element of the array, and decrements the array’s length </li></ul></ul><ul><ul><li>myArray.toString() returns a string containing the values of the array elements, separated by commas </li></ul></ul>
  21. 21. Exception Handling 1 <ul><li>Exception handling in JavaScript is almost the same as in Java </li></ul><ul><li>throw expression creates and throws an exception </li></ul><ul><ul><li>The expression is the value of the exception, and can be of any type (often, it's a literal String) </li></ul></ul><ul><li>try { statements to try } catch (e) { // Notice: no type declaration for e exception-handling statements } finally { // optional, as usual code that is always executed } </li></ul><ul><ul><li>With this form, there is only one catch clause </li></ul></ul>
  22. 22. Exception Handling 2 <ul><li>try { statements to try } catch (e if test1) { exception-handling for the case that test1 is true } catch (e if test2) { exception-handling for when test1 is false and test2 is true } catch (e) { exception-handling for when both test1and test2 are false } finally { // optional, as usual code that is always executed } </li></ul><ul><li>Typically, the test would be something like e == &quot;InvalidNameException&quot; </li></ul>
  23. 23. JavaScript Popup Boxes <ul><li>Alert box </li></ul><ul><ul><ul><li>User will have to click &quot;OK&quot; to proceed </li></ul></ul></ul><ul><ul><ul><li>alert(&quot;sometext&quot;) </li></ul></ul></ul><ul><li>Confirm box </li></ul><ul><ul><ul><li>User will have to click either &quot;OK&quot; or &quot;Cancel&quot; to proceed </li></ul></ul></ul><ul><ul><ul><li>confirm(&quot;sometext&quot;) </li></ul></ul></ul><ul><li>Prompt box </li></ul><ul><ul><li>User will have to click either &quot;OK&quot; or &quot;Cancel&quot; to proceed after entering an input value </li></ul></ul><ul><ul><li>prompt(&quot;sometext&quot;,&quot;defaultvalue&quot;) </li></ul></ul>
  24. 24. JavaScript Functions <ul><li>JavaScript functions are created by the help of “function” keyword. </li></ul><ul><ul><li>Syntax: </li></ul></ul><ul><ul><ul><li>function function_name(arguments) {statement here} </li></ul></ul></ul><ul><li>A JavaScript function contains some code that will be executed only by an event or by a call to that function </li></ul><ul><ul><li>To keep the browser from executing a script as soon as the page is loaded, you can write your script as a function </li></ul></ul><ul><li>You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external .js file). </li></ul><ul><li>Functions can be defined either <head> or <body> section. As a convention, they are typically defined in the <head> section </li></ul>
  25. 25. Example: JavaScript Function <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul><ul><ul><ul><li>function displaymessage() { </li></ul></ul></ul><ul><ul><ul><li>alert(&quot;Hello World!&quot;) </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li></script> </li></ul></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><form> </li></ul><ul><li><input type=&quot;button&quot; value=&quot;Click me!“ onclick=&quot;displaymessage()&quot; > </li></ul><ul><li></form> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  26. 26. Events & Event Handlers <ul><li>Every element on a web page has certain events which can trigger invocation of event handlers </li></ul><ul><li>Attributes are inserted into HTML tags to define events and event handlers </li></ul><ul><li>Examples of events </li></ul><ul><ul><li>A mouse click </li></ul></ul><ul><ul><li>A web page or an image loading </li></ul></ul><ul><ul><li>Mouse over a hot spot on the web page </li></ul></ul><ul><ul><li>Selecting an input box in an HTML form </li></ul></ul><ul><ul><li>Submitting an HTML form </li></ul></ul><ul><ul><li>A keystroke </li></ul></ul>
  27. 27. Events <ul><li>onabort - Loading of an image is interrupted </li></ul><ul><li>onblur - An element loses focus </li></ul><ul><li>onchange - The content of a field changes </li></ul><ul><li>onclick - Mouse clicks an object </li></ul><ul><li>ondblclick - Mouse double-clicks an object </li></ul><ul><li>onerror - An error occurs when loading a document or an image </li></ul><ul><li>onfocus - An element gets focus </li></ul><ul><li>onkeydown - A keyboard key is pressed </li></ul><ul><li>onkeypress - A keyboard key is pressed or held down </li></ul><ul><li>onkeyup - A keyboard key is released </li></ul><ul><li>onload - A page or an image is finished loading </li></ul><ul><li>onmousedown - A mouse button is pressed </li></ul><ul><li>onmousemove - The mouse is moved </li></ul>
  28. 28. Events <ul><li>onmouseout - The mouse is moved off an element </li></ul><ul><li>onmouseover - The mouse is moved over an element </li></ul><ul><li>onmouseup - A mouse button is released </li></ul><ul><li>onreset - The reset button is clicked </li></ul><ul><li>onresize - A window or frame is resized </li></ul><ul><li>onselect - Text is selected </li></ul><ul><li>onsubmit - The submit button is clicked </li></ul><ul><li>onunload - The user exits the page </li></ul>
  29. 29. onSubmit <ul><li>The onSubmit event is used to validate all form fields before submitting it. </li></ul><ul><li>Example: The checkForm() function will be called when the user clicks the submit button in the form. If the field values are not accepted, the submit should be canceled. The function checkForm() returns either true or false. If it returns true the form will be submitted, otherwise the submit will be cancelled: </li></ul><ul><ul><ul><li><form method=&quot;post&quot; action=&quot;xxx.html“ onsubmit=&quot;return checkForm() &quot;> </li></ul></ul></ul>
  30. 30. Example & Demo: onSubmit <ul><li><html> </li></ul><ul><ul><li><head> </li></ul></ul><ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul></ul><ul><ul><ul><ul><li>function validate() { </li></ul></ul></ul></ul><ul><ul><ul><ul><li>// return true or false based on validation logic </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul><ul><ul><li></head> </li></ul></ul><ul><li><body> </li></ul><ul><li><form action=&quot;tryjs_submitpage.htm&quot; onsubmit=&quot;return validate()&quot;> </li></ul><ul><ul><li>Name (max 10 chararcters): <input type=&quot;text&quot; id=&quot;fname&quot; size=&quot;20&quot;><br /> </li></ul></ul><ul><ul><li>Age (from 1 to 100): <input type=&quot;text&quot; id=&quot;age&quot; size=&quot;20&quot;><br /> </li></ul></ul><ul><ul><li>E-mail: <input type=&quot;text&quot; id=&quot;email&quot; size=&quot;20&quot;><br /> </li></ul></ul><ul><ul><li><br /> </li></ul></ul><ul><ul><li><input type=&quot;submit&quot; value=&quot;Submit&quot;> </li></ul></ul><ul><li></form> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  31. 31. Example & Demo: onblur <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul><ul><ul><ul><li>function upperCase() { </li></ul></ul></ul><ul><ul><ul><ul><li>var x=document.getElementById(&quot;fname&quot;).value </li></ul></ul></ul></ul><ul><ul><ul><ul><li>document.getElementById(&quot;fname&quot;).value=x.toUpperCase() </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li></script> </li></ul></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li>Enter your name: </li></ul><ul><li><input type=&quot;text&quot; id=&quot;fname&quot; onblur=&quot;upperCase()&quot;> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  32. 32. JavaScript Object <ul><li>JavaScript is an Object Oriented Programming (OOP) language </li></ul><ul><li>A JavaScript object has properties and methods </li></ul><ul><ul><li>Example: String JavaScript object has length property and toUpperCase() method </li></ul></ul><ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul></ul><ul><ul><ul><ul><li>var txt=&quot;Hello World!&quot; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>document.write(txt.length) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>document.write(txt.toUpperCase()) </li></ul></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul>
  33. 33. JavaScript Built-in Objects <ul><li>Array object </li></ul><ul><li>Boolean object </li></ul><ul><li>Date object </li></ul><ul><li>Math object </li></ul><ul><li>Number object </li></ul><ul><li>String object </li></ul><ul><li>Window object </li></ul><ul><li>Navigator object </li></ul><ul><li>Screen object </li></ul><ul><li>History object </li></ul><ul><li>Location object </li></ul>
  34. 34. Creating Your Own JavaScript Objects <ul><li>3 different ways </li></ul><ul><ul><li>Create a direct instance of an object by using built-in constructor for the Object class </li></ul></ul><ul><ul><li>Create a template (Constructor) first and then create an instance of an object from it </li></ul></ul><ul><ul><li>Create object instance as Hash Literal </li></ul></ul>
  35. 35. Option 1: Creating a Direct Instance of a JavaScript Object <ul><li>By invoking the built-in constructor for the Object class </li></ul><ul><ul><li>personObj=new Object(); // Initially empty with no properties or methods </li></ul></ul><ul><li>Add properties to it </li></ul><ul><ul><ul><li>personObj.firstname=&quot;John&quot;; </li></ul></ul></ul><ul><ul><ul><li>personObj.age=50; </li></ul></ul></ul><ul><li>Add an anonymous function to the personObj </li></ul><ul><ul><ul><li>personObj.tellYourage=function(){ </li></ul></ul></ul><ul><ul><ul><li>alert(“This age is ” + this.age); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>// You can call then tellYourage function as following personObj.tellYourage(); </li></ul></ul></ul>
  36. 36. Option 1:Creating a Direct Instance of a JavaScript Object <ul><li>Add a pre-defined function </li></ul><ul><ul><ul><li>function tellYourage(){ </li></ul></ul></ul><ul><ul><ul><li>alert(“The age is” + this.age); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>personObj.tellYourage=tellYourage; </li></ul></ul></ul><ul><li>Note that the following two lines of code are doing completely different things </li></ul><ul><ul><ul><li>// Set property with a function </li></ul></ul></ul><ul><ul><ul><li>personObj.tellYourage=tellYourage; </li></ul></ul></ul><ul><ul><ul><li>// Set property with returned value of the function </li></ul></ul></ul><ul><ul><ul><li>personObj.tellYourage=tellYourage(); </li></ul></ul></ul>
  37. 37. Option 2: Creating a template of a JavaScript Object <ul><li>The template defines the structure of a JavaScript object in the form of a function </li></ul><ul><li>You can think of the template as a constructor </li></ul><ul><ul><ul><li>function Person(firstname,lastname,age,eyecolor) { </li></ul></ul></ul><ul><ul><ul><ul><li>this.firstname=firstname; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>this.lastname=lastname; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>this.age=age; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>this.tellYourage=function(){ </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>alert(“This age is ” + this.age); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  38. 38. Option 2: Creating a template of a JavaScript Object <ul><li>Once you have the template, you can create new instances of the object </li></ul><ul><ul><ul><li>myFather=new Person(&quot;John&quot;,&quot;Doe&quot;,50,&quot;blue&quot;); </li></ul></ul></ul><ul><ul><ul><li>myMother=new Person(&quot;Sally&quot;,&quot;Rally&quot;,48,&quot;green&quot;); </li></ul></ul></ul><ul><li>You can add new properties and functions to new objects </li></ul><ul><ul><ul><li>myFather.newField = “some data”; </li></ul></ul></ul><ul><ul><ul><li>myFather.myfunction = function() { </li></ul></ul></ul><ul><ul><ul><li>alert(this[&quot;fullName&quot;] + ” is ” + this.age); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  39. 39. Option 3: Creating JavaScript Object as a Hash Literal <ul><li>Create personObj JavaScript object </li></ul><ul><ul><ul><li>var personObj = { </li></ul></ul></ul><ul><ul><ul><ul><li>firstname: &quot;John&quot;, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>lastname: &quot;Doe&quot;, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>age: 50, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>tellYourage: function () { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>alert(“The age is ” + this.age ); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>tellSomething: function(something) { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>alert(something); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>personObj.tellYourage(); </li></ul></ul><ul><ul><li>personObj.tellSomething(“Life is good!”); </li></ul></ul>
  40. 40. HTML DOM <ul><li>The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate HTML documents </li></ul><ul><li>All HTML elements, along with their containing text and attributes, can be accessed through the DOM. </li></ul><ul><ul><li>The contents can be modified or deleted, and new elements can be created. </li></ul></ul><ul><li>JavaScript uses the HTML Document Object Model to manipulate HTML. </li></ul><ul><li>Levels of the DOM are dot-separated in the syntax. </li></ul>
  41. 41. HTML DOM Objects <ul><li>Anchor object </li></ul><ul><li>Document object </li></ul><ul><li>Event object </li></ul><ul><li>Form and Form Input object </li></ul><ul><li>Frame, Frameset, and IFrame objects </li></ul><ul><li>Image object </li></ul><ul><li>Location object </li></ul><ul><li>Navigator object </li></ul><ul><li>Option and Select objects </li></ul><ul><li>Screen object </li></ul><ul><li>Table, TableHeader, TableRow, TableData objects </li></ul><ul><li>Window object </li></ul>
  42. 42. Document Tree Structure
  43. 43. Document Object: Write text to the output <ul><li><html> </li></ul><ul><ul><li><body> </li></ul></ul><ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul></ul><ul><ul><ul><li>document.write(“<h1>Hello World!</h1>&quot;) </li></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul><ul><ul><li></body> </li></ul></ul><ul><li></html> </li></ul>
  44. 44. Document Object: Use getElementById() <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul><ul><ul><ul><li>function getElement() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><ul><li>var x=document.getElementById(&quot;myHeader&quot;) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>alert(&quot;I am a &quot; + x.tagName + &quot; element&quot;) </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li></script> </li></ul></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><h1 id=&quot;myHeader&quot; onclick=&quot;getElement()&quot;>Click to see what element I am!</h1> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  45. 45. Document Object: Use getElementsByName() <ul><li><html> </li></ul><ul><ul><li><head> </li></ul></ul><ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul></ul><ul><ul><ul><ul><li>function getElements() { </li></ul></ul></ul></ul><ul><ul><ul><ul><li>var x=document.getElementsByName(&quot;myInput&quot;) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>alert(x.length + &quot; elements!&quot;) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul><ul><ul><li></head> </li></ul></ul><ul><li><body> </li></ul><ul><li><input name=&quot;myInput&quot; type=&quot;text&quot; size=&quot;20&quot;><br /> </li></ul><ul><li><input name=&quot;myInput&quot; type=&quot;text&quot; size=&quot;20&quot;><br /> </li></ul><ul><li><input name=&quot;myInput&quot; type=&quot;text&quot; size=&quot;20&quot;><br /> </li></ul><ul><li><br /> </li></ul><ul><li><input type=&quot;button&quot; onclick=&quot;getElements()&quot; value=&quot;How many elements named 'myInput'?&quot;> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  46. 46. Return the innerHTML of the first anchor in a document <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><ul><li><a name=&quot;first&quot;>First anchor</a><br /> </li></ul></ul><ul><ul><li><a name=&quot;second&quot;>Second anchor</a><br /> </li></ul></ul><ul><ul><li><a name=&quot;third&quot;>Third anchor</a><br /> </li></ul></ul><ul><ul><li><br /> </li></ul></ul><ul><ul><li>InnerHTML of the first anchor in this document: </li></ul></ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul><ul><ul><li>document.write(document.anchors[0].innerHTML) </li></ul></ul><ul><ul><li></script> </li></ul></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  47. 47. Event Object: What are the coordinates of the cursor? <ul><li><html> </li></ul><ul><ul><li><head> </li></ul></ul><ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul></ul><ul><ul><ul><li>function show_coords(event) { </li></ul></ul></ul><ul><ul><ul><li>x=event.clientX </li></ul></ul></ul><ul><ul><ul><li>y=event.clientY </li></ul></ul></ul><ul><ul><ul><li>alert(&quot;X coords: &quot; + x + &quot;, Y coords: &quot; + y) </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul><ul><ul><li></head> </li></ul></ul><ul><li><body onmousedown=&quot;show_coords(event)&quot;> </li></ul><ul><li><p>Click in the document. An alert box will alert the x and y coordinates of the </li></ul><ul><li>cursor.</p> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  48. 48. Event Object: What is the unicode of the key pressed? <ul><li><html> </li></ul><ul><ul><li><head> </li></ul></ul><ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul></ul><ul><ul><ul><li>function whichButton(event) { </li></ul></ul></ul><ul><ul><ul><li>alert(event.keyCode) </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul><ul><ul><li></head> </li></ul></ul><ul><li><body onkeyup=&quot;whichButton(event)&quot;> </li></ul><ul><li><p><b>Note:</b> Make sure the right frame has focus when trying this example!</p> </li></ul><ul><li><p>Press a key on your keyboard. An alert box will alert the unicode of the key </li></ul><ul><li>pressed.</p> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  49. 49. Event Object: Which event type occurred? <ul><ul><li><html> </li></ul></ul><ul><ul><li><head> </li></ul></ul><ul><ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul></ul></ul><ul><ul><ul><ul><li>function whichType(event) { </li></ul></ul></ul></ul><ul><ul><ul><ul><li>alert(event.type) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul><ul><ul><li></head> </li></ul></ul><ul><ul><li><body onmousedown=&quot;whichType(event)&quot;> </li></ul></ul><ul><ul><li><p> </li></ul></ul><ul><ul><li>Click on the document. An alert box will alert which type of event occurred. </li></ul></ul><ul><ul><li></p> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  50. 50. Questions
  51. 51. Thank you for your Time and Attention! For more information visit Or drop-in an email to