04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: The Document Object Model<br />

In addition to these methods, the length property returns the number of characters in the node. This<br />

value is the same as using nodeValue.length or data.length .<br />

By default, every element that may contain content will have at most one text node when content is<br />

present. Here is an example:<br />

< !-- no content, so no text node -- ><br />

< div > < /div ><br />

< !-- white space content, so one text node -- ><br />

< div > < /div ><br />

< !-- content, so one text node -- ><br />

< div > Hello World! < /div ><br />

The first < div > element in this code has no content, so there is no text node. Any content between the<br />

opening and closing tags means that a text node must be created, so the second < div > element has a<br />

single text node as a child even though its content is white space. The text node ’ s nodeValue is a single<br />

space. The third < div > also has a single text node whose nodeValue is “ Hello World! ” . The following<br />

code lets you access this node:<br />

var textNode = div.firstChild; //or div.childNodes[0]<br />

Once a reference to the text node is retrieved, it can be changed like this:<br />

div.firstChild.nodeValue = “Some other message”<br />

As long as the node is currently in the document tree, the changes to the text node will be reflected<br />

immediately. Another note about changing the value of a text node is that the string is HTML - or XML -<br />

encoded (depending on the type of document), meaning that any less - than symbols, greater - than<br />

symbols, or quotation marks are escaped as shown in this example:<br />

//outputs as “Some & lt;strong & gt;other & lt;/strong & gt; message”<br />

div.firstChild.nodeValue = “Some < strong > other < /strong > message”;<br />

This is an effective way of HTML - encoding a string before inserting it into the DOM document.<br />

The Text type constructor and prototype are accessible in script in all browsers, including Internet<br />

Explorer beginning with version 8.<br />

Creating Text Nodes<br />

New text nodes can be created using the document.createTextNode() method, which accepts a single<br />

argument — the text to be inserted into the node. As with setting the value of an existing text node, the<br />

text will be HTML - or XML - encoded as shown in this example:<br />

var textNode = document.createTextNode(“ < strong > Hello < /strong > world!”);<br />

When a new text node is created, its ownerDocument property is set, but it does not appear in the<br />

browser window until it is added to a node in the document tree. The following code creates a new<br />

< div > element and adds a message to it:<br />

290

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

Saved successfully!

Ooh no, something went wrong!