chatko
10-20-2009, 03:27 AM
Hi guys,
I'm trying to inject javascript into a dynamically created iframe's <head> element. My approach works in IE8, firefox 3+ and Chrome. It breaks IE7 with a "htmlfile: invalid argument." message. I use the following function:
(where, element is my iframe, url is the script url and mytype= "text/javascript"
function InjectScript(element, url, callback, myType){
var headID = element.doc.getElementsByTagName("head")[0];
if (!headID) { return; }
var script = document.createElement('script');
script.type = myType;
script.src = url;
if (script.readyState){ //IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else {
script.onload = function ()
{ // FireFox
callback();
}
}
headID.appendChild(script);
}
In the microsoft debugger, I can see that the error occurs on "headID.appendChild(script);". Both the HeadID and script elements look good in the debugger. I can also alert their respective properties at runtime before the call to appendChild.
Any ideas why this fails in IE7 and whatI can do to fix?
Thanks,
Chris
I'm trying to inject javascript into a dynamically created iframe's <head> element. My approach works in IE8, firefox 3+ and Chrome. It breaks IE7 with a "htmlfile: invalid argument." message. I use the following function:
(where, element is my iframe, url is the script url and mytype= "text/javascript"
function InjectScript(element, url, callback, myType){
var headID = element.doc.getElementsByTagName("head")[0];
if (!headID) { return; }
var script = document.createElement('script');
script.type = myType;
script.src = url;
if (script.readyState){ //IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else {
script.onload = function ()
{ // FireFox
callback();
}
}
headID.appendChild(script);
}
In the microsoft debugger, I can see that the error occurs on "headID.appendChild(script);". Both the HeadID and script elements look good in the debugger. I can also alert their respective properties at runtime before the call to appendChild.
Any ideas why this fails in IE7 and whatI can do to fix?
Thanks,
Chris