Download presentation
Presentation is loading. Please wait.
Published byNoel Allen Modified over 9 years ago
1
(1) JavaScript
2
(2) JavaScript vs. Java JavaScript and Java are completely different Java, invented by Sun in 1995, is a complex programming language and platform JavaScript, invented by Brendan Eich in 1995, is a scripting language. - First used by the Netscape browser - Adopted by ECMA in 1997 - Provides access the the HTML Document Object Model (DOM)
3
(3) Document Object Model Platform and language neutral interface Allows programs and scripts to dynamically access and update: Content Structure Style of HTML documents
4
(4) DOM Represents the document as a forest of Nodes foo bar baz qux foobarbazqux
5
(5) DOM Interfaces DOMException only raised in ‘exceptional’ cases Document : Node DocumentType, DOMImplementation, Element Element createElement(tag) raises DOMException Text createTextNode(data) Comment createComment(data) Attr createAttribute(name) raises DOMException Element getElementById(id) NodeList getElementsByTagName(name) NodeList getElementsByClassName(name)
6
(6) DOM Interface Node NodeType, parentNode, childNodes Node insertBefore(newChild, refChild) raises DOMException Node replaceChild(newChild, oldChild) raises DOMException Node removeChild(oldChild) raises DOMException Node appendChild(newChild) raises DOMException boolean hasChildNodes()
7
(7) DOM Interface Element DOMString getAttribute(name) setAttribute(name, value) Attr getAttributeNode(name) Attr setAttributeNode(newAttr) NodeList getElementsByTagName(name)
8
(8) JavaScript Placed inside tags no longer need type=“text/javascript” tags can be in or loads external script file
9
(9) JavaScript Structure Statements tell the browser what to do var x=3; Should end with ; Code is a sequence of JavaScript statements Blocks of code is grouped with {} JavaScript is case sensitive myVar != myvar Uses C++ comments // and /* */
10
(10) JavaScript 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}
11
(11) JavaScript Functions Functions use the ‘function’ keyword function myFunction(name, job) { alert(“Welcome “ + name + “, the “ + job); var x=5; return x; }
12
(12) JavaScript Operators Arithmetic: +, -, *, /, %, ++, -- Assignment: =, +=, -=, *=, /=, %= Comparison: ==, ===, !=, >, =, <= Logical: &&, ||, ! Condition: x=(condition)?value1:value2;
13
(13) Condition Statements if(condition){…} if(condition){…}else{…} if(condition){…}else if(condition2){…}else{…} switch(n){case 1:…break;…;default:…}
14
(14) 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
15
(15) JavaScript Labels Similar to goto statements (EVIL) label: … break label; … continue label;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.