"> ">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Set 2: DOM IT452 Advanced Web and Internet Systems.

Similar presentations


Presentation on theme: "Set 2: DOM IT452 Advanced Web and Internet Systems."— Presentation transcript:

1 Set 2: DOM IT452 Advanced Web and Internet Systems

2 Review – DHTML Basics Find the HTML object we want to change var domLink = document.getElementById("linkToAnimal"); Change the object’s: –HTML properties domLink.href = "cat.html"; –CSS properties domLink.style.backgroundColor = "blue";

3 Simple Paint (ex1.html) – Part 1 IT 452 DOM demo <!-- var currentAction = "red"; function doupdate(curNode ) { curNode.style.backgroundColor = currentAction; } // -->

4 Simple Paint (ex1.html) – Part 2 IT 452 DOM demo Tools: Set red Set blue thing 1 thing 2 thing 3 thing 4

5 What does the DOM tree look like?

6 ex2.html – add insert/delete/edit function doupdate(curNode ) { var parent = curNode.parentNode; if (currentAction == "insert") { newThing = document.createElement("td"); parent.insertBefore(newThing, curNode); newThing.innerHTML = "hello"; } else if (currentAction == "delete") { parent.removeChild(curNode); } else if (currentAction == "edit") { curNode.innerHTML = window.prompt("Enter new text", ""); } else { curNode.style.backgroundColor = currentAction; }

7 ex3.html – fully functional function doupdateDyn() { doupdate(this); } function doupdate(curNode ) { var parent = curNode.parentNode; if (currentAction == "insert") { newThing = document.createElement("td"); parent.insertBefore(newThing, curNode); newThing.innerHTML = "hello"; newThing.onclick = doupdateDyn; } else if (currentAction == "delete") { parent.removeChild(curNode); } else if (currentAction == "edit") { curNode.innerHTML = window.prompt("Enter new text", curNode.innerHTML); } else { curNode.style.backgroundColor = currentAction; }

8 Event Variables Require Event Types 1.Javascript is weakly typed 2.A given variable can be assigned strings, ints, events, etc. 3.However, variables can be assumed to be a certain type, used later by other code. newThing.href assumes a string newThing.onclick assumes an event If you violate these assumptions, something will likely break later on your webpage.

9 Creating an onclick event for a node. Bad Ideas newThing.onclick = "doupdate(this)“; newThing.onclick = doupdateDyn(); Good Ideas newThing.onclick = doupdateDyn;

10 Three Ways to do this newThing.onclick = doupdateDyn; newThing.addEventListener(‘click’, doupdateDyn); newThing.onclick = function() { doupdate(this); }

11 ex4.html – more elegant / alternatives function doupdate(curNode ) { var parent = curNode.parentNode; if (currentAction == "insert") { newThing = document.createElement("td"); parent.insertBefore(newThing, curNode); newThing.innerHTML = "hello"; newThing.onclick = function() {doupdate(this)}; } else if (currentAction == "delete") { parent.removeChild(curNode); } else if (currentAction == "edit") { curNode.firstChild.nodeValue = window.prompt("Enter new text", curNode.innerHTML); } else { curNode.style.backgroundColor = currentAction; }

12 Summary Grab HTML elements document.getElementById(“yourid”) document.getElementsByTagName(“p”) node.childNodes node.parentNode Create new HTML elements node = document.createElement(“p”) node = document.createTextNode(“text that goes in a p later”) Add new HTML elements to the page node.appendChild(newnode) node.insertBefore(newnode, node) Change HTML attributes/style yourvar.href = “somelink.html” yourvar.style.styleName = “style-val”

13 Tips Remember JavaScript console How many children? –Use DOM Inspector to see (Firefox and Chrome) –From ex5.html: else if (currentAction == "parent") { var kids = parent.childNodes; window.alert("Number children: "+kids.length); window.alert("Type of first: "+kids[0].nodeType); } See DOM reference links under IT452 page –Use the API!

14 Ex6.html: iterating over children var kids = parent.childNodes; for (var i=0; i < kids.length; i++) { var theKid = kids[i]; window.alert("Kid #“ + i + " has type“ + theKid.nodeType); }


Download ppt "Set 2: DOM IT452 Advanced Web and Internet Systems."

Similar presentations


Ads by Google