Java Script 1 Java Script vs Java Script

  • Slides: 15
Download presentation
Java. Script (1)

Java. Script (1)

Java. Script vs. Java. Script and Java are completely different • Java, invented by

Java. Script vs. Java. Script and Java are completely different • Java, invented by Sun in 1995, is a complex programming language and platform • Java. Script, invented by Brendan Eich in 1995, is a scripting language. - First used by the Netscape browser - Adopted by ECMA in 1997 - Provides access the HTML Document Object Model (DOM) (2)

Document Object Model Platform and language neutral interface Allows programs and scripts to dynamically

Document Object Model Platform and language neutral interface Allows programs and scripts to dynamically access and update: • Content • Structure • Style of HTML documents (3)

DOM Represents the document as a forest of Nodes <table> <tbody> <tr> <td>foo</td> <td>bar</td>

DOM Represents the document as a forest of Nodes <table> <tbody> <tr> <td>foo</td> <td>bar</td> </tr> <td>baz</td> <td>qux</td> </tr> </tbody> </table> (4) <table> <tbody> <tr> <td> foo bar baz qux

DOM Interfaces DOMException only raised in ‘exceptional’ cases Document : Node • Document. Type,

DOM Interfaces DOMException only raised in ‘exceptional’ cases Document : Node • Document. Type, DOMImplementation, Element • • (5) Element create. Element(tag) raises DOMException Text create. Text. Node(data) Comment create. Comment(data) Attr create. Attribute(name) raises DOMException Element get. Element. By. Id(id) Node. List get. Elements. By. Tag. Name(name) Node. List get. Elements. By. Class. Name(name)

DOM Interface Node • Node. Type, parent. Node, child. Nodes • Node insert. Before(new.

DOM Interface Node • Node. Type, parent. Node, child. Nodes • Node insert. Before(new. Child, ref. Child) raises • • (6) DOMException Node replace. Child(new. Child, old. Child) raises DOMException Node remove. Child(old. Child) raises DOMException Node append. Child(new. Child) raises DOMException boolean has. Child. Nodes()

DOM Interface Element • DOMString get. Attribute(name) • set. Attribute(name, value) • Attr get.

DOM Interface Element • DOMString get. Attribute(name) • set. Attribute(name, value) • Attr get. Attribute. Node(name) • Attr set. Attribute. Node(new. Attr) • Node. List get. Elements. By. Tag. Name(name) (7)

Java. Script Placed inside <script></script> tags • no longer need type=“text/javascript” • <script> tags

Java. Script Placed inside <script></script> tags • no longer need type=“text/javascript” • <script> tags can be in <head> or <body> • <script src=“filename. js”></script> loads external script file (8)

Java. Script Structure Statements tell the browser what to do • var x=3; Should

Java. Script Structure Statements tell the browser what to do • var x=3; Should end with ; Code is a sequence of Java. Script statements Blocks of code is grouped with {} Java. Script is case sensitive • my. Var != myvar Uses C++ comments // and /* */ (9)

Java. Script Types Strings • var fname=‘Cam’; var lname=“Moore”; Numbers • var x=3; var

Java. Script Types Strings • var fname=‘Cam’; var lname=“Moore”; Numbers • var x=3; var y=2. 1; Boolean • true, false Arrays • var foo=Array(); var bar=[‘a’, ‘b’, 3]; Objects • var person={fname: ”John”, lname: ”Doe”, id=3} (10)

Java. Script Functions use the ‘function’ keyword • function my. Function(name, job) { }

Java. Script Functions use the ‘function’ keyword • function my. Function(name, job) { } (11) alert(“Welcome “ + name + “, the “ + job); var x=5; return x;

Java. Script Operators Arithmetic: • +, -, *, /, %, ++, -Assignment: • =,

Java. Script Operators Arithmetic: • +, -, *, /, %, ++, -Assignment: • =, +=, -=, *=, /=, %= Comparison: • ==, ===, !=, >, <, >=, <= Logical: • &&, ||, ! Condition: • x=(condition)? value 1: value 2; (12)

Condition Statements • if(condition){…}else{…} • if(condition){…}else if(condition 2){…}else{…} • switch(n){case 1: …break; …; default:

Condition Statements • if(condition){…}else{…} • if(condition){…}else if(condition 2){…}else{…} • switch(n){case 1: …break; …; default: …} (13)

Loops • for(init; condition; increment) {…} - Classic for loop • for(x in person)

Loops • for(init; condition; increment) {…} - Classic for loop • for(x in person) {…} - Loops over all properties in person • while(condition){…} - Classic while loop • do{…}while(condition); - Classic do/while loop (14)

Java. Script Labels Similar to goto statements (EVIL) label: … break label; … continue

Java. Script Labels Similar to goto statements (EVIL) label: … break label; … continue label; (15)