18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6<br />

Method Description IE MOZ OP SAF<br />

createEntity Creates an entity reference node – X – –<br />

Reference(name) with the given name<br />

createProcessing Creates a PI node with the – X – –<br />

Instruction(target, given target and data<br />

data)<br />

createTextNode(text) Creates a text node containing text X X X X<br />

IE = Internet Explorer 6 for Windows, MOZ = Mozilla 1.5 for all platforms, OP = Opera 7.5 for all platforms,<br />

SAF = Safari 1.2 for MacOS.<br />

The most commonly used methods are createDocumentFragment(), createElement(), and<br />

createTextNode(); the other methods are either not useful (createComment()) or not supported by<br />

enough browsers to be useful at this point in time.<br />

createElement(), createTextNode(), appendChild()<br />

Suppose you have the following HTML page:<br />

<br />

<br />

createElement() Example<br />

<br />

<br />

<br />

<br />

To this page, you want to add the following code using the DOM:<br />

Hello World!<br />

The createElement() and createTextNode() methods can be used to accomplish this. Here’s how.<br />

The first thing to do is create the element:<br />

var oP = document.createElement(“p”);<br />

Second, create the text node:<br />

var oText = document.createTextNode(“Hello World!”);<br />

Next you need to add the text node to the element. To do this, you can use the appendChild() method,<br />

which was briefly mentioned earlier in the chapter. The appendChild() method exists on every node<br />

type and is used to add a given node to the end of another’s childNodes list. In this case, the text node<br />

should be added to the element:<br />

oP.appendChild(oText);<br />

174

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!