04.11.2015 Views

javascript

Create successful ePaper yourself

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

Chapter 10: The Document Object Model<br />

The exact text returned from innerHTML differs from browser to browser. IE and Opera tend to convert<br />

all tags to uppercase, whereas Safari, Chrome, and Firefox return HTML in the way it is specified in the<br />

document, including white space and indentation. You cannot depend on the returned value of<br />

innerHTML being exactly the same from browser to browser.<br />

When used in write mode, innerHTML parses the given string into a DOM subtree and replaces all of the<br />

existing child nodes with it. Because the string is considered to be HTML, all tags are converted into<br />

elements in the standard way that the browser handles HTML (again, this differs from browser to<br />

browser). Setting simple text without any HTML tags, as shown here, acts the same as innerText :<br />

div.innerHTML = “Hello world!”;<br />

Setting innerHTML to a string containing HTML behaves quite differently. Where innerText escaped<br />

HTML syntax characters, innerHTML parses them. Consider the following example:<br />

div.innerHTML = “Hello & welcome, < b > \”reader\”! < /b > ”;<br />

The result of this operation is as follows:<br />

< div id=”content” > Hello & amp; welcome, < b > & quot;reader & quot;! < /b > < /div ><br />

After setting innerHTML , you can access the newly created nodes as you would any other nodes in the<br />

document.<br />

Setting innerHTML causes the HTML string to be parsed by the browser into an appropriate DOM<br />

tree. This means that setting innerHTML and then reading it back typically results in a different string<br />

being returned. This is because the returned string is the result of serializing the DOM subtree that was<br />

created for the original HTML string.<br />

There are some limitations to innerHTML . For one, < script > elements cannot be executed when<br />

inserted via innerHTML in most browsers. IE is the only browser that allows this, but only as long as the<br />

defer attribute is specified and the < script > element is preceded by what Microsoft calls a scoped<br />

element . The < script > element is considered a NoScope element , which more or less means that it has no<br />

visual representation on the page, like a < style > element or a comment. IE strips out all NoScope<br />

elements from the beginning of strings inserted via innerHTML , which means the following won ’ t work:<br />

div.innerHTML = “ < script defer > alert(‘hi’); < /scr” + “ipt > ”; //won’t work<br />

In this case, the innerHTML string begins with a NoScope element, so the entire string becomes empty.<br />

To allow this script to work appropriately, it must be preceded by a scoped element, such as a text node<br />

or an element without a closing tag such as < input > . The following lines will all work:<br />

div.innerHTML = “_ < script defer > alert(‘hi’); < /scr” + “ipt > ”;<br />

div.innerHTML = “ < div > & nbsp; < /div > < script defer > alert(‘hi’); < /scr” + “ipt > ”;<br />

div.innerHTML = “ < input type=\”hidden\” > < script defer > alert(‘hi’); < /scr” + “ipt > ”;<br />

The first line results in a text node being inserted immediately before the < script > element. You may<br />

need to remove this after the fact so as not to disrupt the flow of the page. The second line is a similar<br />

approach, using a < div > element with a nonbreaking space. An empty < div > alone won ’ t do the trick; it<br />

must contain some content that will force a text node to be created. Once again, the first node may need<br />

303

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

Saved successfully!

Ooh no, something went wrong!