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 />

to be removed to avoid layout issues. The third line uses a hidden < input > field to accomplish the same<br />

thing. Since it doesn ’ t affect the layout of the page, this may be the optimal case for most situations.<br />

In most browsers, the < style > element causes similar problems with innerHTML . Opera 9 and later as<br />

well as Firefox 2 and later support the insertion of < style > elements using innerHTML in the exact way<br />

you ’ d expect, as shown here:<br />

div.innerHTML = “ < style type=\”text/css\” > body {background-color: red; } < /style > ”;<br />

IE and Safari ignore the < style > element. In IE, < style > is yet another No Scope element, so it must be<br />

preceded by a scoped element such as this:<br />

div.innerHTML = “_ < style type=\”text/css\” > body {background-color: red; } < /style > ”;<br />

div.removeChild(div.firstChild);<br />

Safari and Chrome continue to ignore the < style > element because it ’ s not attached to the < head ><br />

element. So, to make this work in all four browsers, the following code must be used:<br />

//Opera, Firefox, and IE<br />

div.innerHTML = “_ < style type=\”text/css\” > body {background-color: red; } < /style > ”;<br />

div.removeChild(div.firstChild);<br />

//Safari and Chrome<br />

document.getElementsByTagName(“head”)[0].appendChild(div.firstChild);<br />

When you add the newly created < style > element to the < head > , Safari and Chrome honor the new<br />

style information.<br />

The innerHTML property is not available on all elements. The following elements do not support<br />

innerHTML : < col > , < colgroup > , < frameset > , < head > , < html > , < style > , < table > , < tbody > ,<br />

< thead > , < tfoot > , < title > , and < tr > .<br />

Firefox ’s support of innerHTML is stricter in XHTML documents served<br />

with the application/xhtml+xml content type. When using innerHTML<br />

in XHTML documents, you must specify well - formed XHTML code. If the<br />

code is not well -formed, setting innerHTML fails silently.<br />

Whenever you ’ re using innerHTML to insert HTML from a source external to your code, it ’ s important to<br />

sanitize the HTML before passing it through to innerHTML . IE 8 added the window.toStaticHTML() method<br />

for this purpose. This method takes a single argument, an HTML string, and returns a sanitized version<br />

that has all script nodes and script event - handler attributes removed from the source. Following is an example:<br />

var text = “ < a href=\”#\” onclick=\”alert(‘hi’\” > Click Me < /a > ”;<br />

var sanitized = window.toStaticHTML(text); //IE 8 only<br />

alert(sanitized); //” < a href=\”#\” > Click Me < /a > ”<br />

This example runs an HTML link string through toStaticHTML() . The sanitized text no longer has the<br />

onclick attribute present. Though IE 8 is the only browser with this native functionality, it is still<br />

advisable to be careful when using innerHTML and inspect the text manually before inserting it, if possible.<br />

304

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

Saved successfully!

Ooh no, something went wrong!