I've been learning about the pros and cons of the technique I used. It might be that the following is better, I'm not sure - I think this needs much testing on a variety of browsers/OSs.
Code:
function init()
{
//
var c = "<h1>Test<\/h1><script type='text/javascript'>function foo(){alert('In foo()');}<\/script><p>This is a test.<\/p>";
var d = document.getElementById('div1');
d.innerHTML = c;
//
var i, t, se, sa = d.getElementsByTagName('script');
for (i = 0; i < sa.length; ++i) {
se = document.createElement('script');
se.type = 'text/javascript';
/*@cc_on
@if (@_jscript)
se.text = sa[i].text;
@else @*/
t = document.createTextNode(sa[i].innerHTML); // modified per 7stud's test results below
se.appendChild(t);
/*@end @*/
document.getElementsByTagName('head')[0].appendChild(se);
d.removeChild(sa[i]); // optional
}
}
Jim, I thought about alternatives too - but they all have pros and cons. In this case I can see why Pete doesn't want to make changes to "a very complex XSLT layer".
Edit:
This function changed as the discussion progressed. For the latest code, see posts later in the thread and the online demo.
Bookmarks