Dom
Dom
• Suggested Solution
• • Hint:
• • Get the value of the attribute href from the link
• • Find the placeholder node •
• Set the src attribute of the placeholder to be the same value as the href attribute of the link
• Code function showPic(whichpic) { var source = whichpic.getAttribute("href");
• var placeholder = document.getElementById("placeholder");
• placeholder.setAttribute("src",source); }
• In each link, we need an onclick event: onclick="showPic(this);return false;"
• Javascript for Functionality Graceful Degradation Progressive Enhancement CSS for
Presentation HTML for Markup Web Page Architecture
• • Graceful Degradation = your site is navigable by users who do not have Javascript
• • Progressive Enhancement = page built in layers: The page should be constructed
using 3 different files – one for each layer
• Does this degrade gracefully? <a href=“#”
onclick=“popUp(‘http://www.example.com’); return false;”)>Link to Example</a> This
does degrade gracefully: <a href=“http://www.example.com”
onclick=“popUp(‘http://www.example.com’); return false;”)>Link to Example</a> But
it is a little clumsy. There is a shortcut: <a href=“http://www.example.com”
onclick=“popUp(this.href); return false;”)>Link to Example</a> Graceful Degradation
• Progressive Enhancement • Graceful Degradation follows from Progressive
Enhancement • We need to separate the Javascript from the markup by removing
the event handlers from the HTML completely • We can attach events to HTML
tags in the Javascript using the attributes class and id
• Unobtrusive Javascript • Examine this code: window.onload = prepareLinks;
function prepareLinks() { var links = document.getElementsByTagName(‘a’); for
(var i=0; i<links.length; i++) { if (links[i].className == “popup”) { links[i].onclick =
function() { popUp(this.getAttribute(“href”)); return false; } } } } Attaches code to
the onclick event of tags which have been identified by their class name
• Using Unobtrusive Javascript • Hands On • Open JavascriptDOM4.html • Remove
the onclick event handlers • In each a tag put the attribute class=“popup” • Enter
the code from the example given so that it is unobtrusively called from the onclick
event • Also enter this code to open a popup window: function popUp(winURL)
{ window.open(winURL,”popup”,”width=320,height=480”); }
• Backwards Compatibility • Although most browsers fully support the DOM, some do not
support it completely. • Browser sniffing is too convoluted, so best to check for specific features
• Put this line of code at the beginning of a function if (!document.getElementsByTagName)
return false; • So, if the browser does not support this method the function will stop
• Tidying Up • Hands On • To follow through the principle of completely separating the three
layers, we need to put all our Javascript in a separate file • Open JavascriptDOM4.html • Put
the Javascript code into a new file called javascriptdom4.js • Put a link into the head: • <script
type=“text/javascript” language=“javascript” src=“javascriptdom4.js” /> • Put checks into the
code to ensure that your page gracefully degrades if the user’s browser does not have sufficient
level of support for Javascript
• innerHTML • Hands On • In the body of a blank HTML page insert a div tag: <div id=“test”>This
will be replaced</div> • In the head of the page place this code: window.onload = function()
{ var testdiv = document.getElementBy Id(“testdiv”); testdiv.innerHTML = “<p>Now we have
inserted <em>this</em> instead!</p>”; }
• Using innerHTML • All the HTML in the tag is replaced when the innerHTML method is
used • innerHTML is not part of the DOM – so it may one day disappear – though it is
universally recognised by browsers • Tags within the innerHTML are not part of the
DOM tree so they cannot be manipulated
• Recap • Hands On • Now let us use the DOM to insert the same HTML into the div tag
• Create an element node “p” assigned to the variable para • Create a text node
assigned to the variable txt1 (‘Now we have inserted’) • Append txt1 to para • Create
an element node em assigned to the variable emphasis • Append emphasis to para •
Create a text node assigned to the variable txt2 (‘this’) • Append txt2 to emaphasis •
Append emphasis to para • Create a text node assigned to the variable txt3 (‘instead!’)
• Append txt3 to para • Append para to the element testdiv in the document
• Javascript and CSS • Hands On • Open file JavascriptDOM6.html and examine the
code • Now try out each of the 3 user events • What do you notice about the
difference between appendChild and insertBefore? Syntax of insertBefore:
parentElement.insertBefore(newElement, targetElement)
• Points about CSS • If style is set inline, it can be manipulated using Javascript • If style is set
by CSS, Javascript cannot directly manipulate it • However, Javascript can set the style of
the element and override the CSS • Also, Javascript can indirectly manipulate style using the
class tag
• Changing the Class Attribute • A useful way of manipulating style through Javascript is to
add a second class to an element eg: thiselement.className += “ newclass”; • adds the
class ‘newclass’ to the class attribute • Hands On • Open file JavascriptDOM7.html • Create
a style for the tags with the class plain • Create a further style with more emphasis called
‘emphatic’ • Write Javascript code that adds the emphatic class to an element as the user
mouseovers and removes it when the user mouseouts
• Objectives • Understand the nature and structure of the DOM • Add and remove content
from the page • Access and change element attributes – including source and class • Insert
markup into a page using innerHTML • Change style attribute using Javascript