Animal
10-10-2006, 09:20 AM
Using innerHTML to insert dynamically loaded code doesn't execute any <script> elements in that code.
Is it possible/worthwhile to search for scripts in the text?
A lot of the content of our system is going to be dynamically loaded into tabs or other containers by code which has no idea about the contents or functionality of the loaded content.
Those loaded pages themselves may need to use YUI, or yui-ext widgets.
As it stands now, dynamically loaded pages cannot use javascript widgets.
Would something like:
YAHOO.ext.Element.update = function(html)
{
this.dom.innerHTML = html;
var s = this.dom.getElementsByTagName("script");
var docHead = document.getElementsByTagName("head")[0];
// For browsers which discard scripts when inserting innerHTML,
// extract the scripts using a RegExp
if (s.length == 0)
{
var re = /(?:<script.*(?:src=[\"\'](.*)[\"\']).*>.*<\/script>)|(?:<script.*>([\S\s]*?)<\/script>)/ig; // assumes HTML well formed and then loop through it.
var match;
while (match = re.exec(html))
{
var s0 = document.createElement("script");
if (match[1])
s0.src = match[1];
else if (match[2])
s0.text = match[2];
else
continue;
docHead.appendChild(s0);
}
}
else
{
for (var i = 0; i < s.length; i++)
{
var s0 = document.createElement("script");
s0.type = s[i].type;
if (s[i].text)
{
s0.text = s[i].text;
}
else
s0.src = s[i].src;
docHead.appendChild(s0);
}
}
};
work?
Is it possible/worthwhile to search for scripts in the text?
A lot of the content of our system is going to be dynamically loaded into tabs or other containers by code which has no idea about the contents or functionality of the loaded content.
Those loaded pages themselves may need to use YUI, or yui-ext widgets.
As it stands now, dynamically loaded pages cannot use javascript widgets.
Would something like:
YAHOO.ext.Element.update = function(html)
{
this.dom.innerHTML = html;
var s = this.dom.getElementsByTagName("script");
var docHead = document.getElementsByTagName("head")[0];
// For browsers which discard scripts when inserting innerHTML,
// extract the scripts using a RegExp
if (s.length == 0)
{
var re = /(?:<script.*(?:src=[\"\'](.*)[\"\']).*>.*<\/script>)|(?:<script.*>([\S\s]*?)<\/script>)/ig; // assumes HTML well formed and then loop through it.
var match;
while (match = re.exec(html))
{
var s0 = document.createElement("script");
if (match[1])
s0.src = match[1];
else if (match[2])
s0.text = match[2];
else
continue;
docHead.appendChild(s0);
}
}
else
{
for (var i = 0; i < s.length; i++)
{
var s0 = document.createElement("script");
s0.type = s[i].type;
if (s[i].text)
{
s0.text = s[i].text;
}
else
s0.src = s[i].src;
docHead.appendChild(s0);
}
}
};
work?