我需要他的代码才能在IE8 +中使用iframe 我有一个字符串中的html代码,但有和没有src-s的脚本在IE8中的范围不同。 所以1.文件是Jquery,但4.文件运行时出错:“jQuery未定义”。 例如:
<script>jQuery.noConflict();</script>
我的代码的重要部分:
var iframe = document.getElementById('my_iframe')
if (frame == null) {
return;
}
iframe.style.display = 'block';
var doc = null;
if (iframe.contentDocument)
doc = iframe.contentDocument;
//IE8!
else if (iframe.contentWindow)
doc = iframe.contentWindow.document;
if (doc === null) {
return;
}
// the content variable has the html in a string, here I get the script elements out of this string
//...
doc.open();
doc.write(content);
if (typeof doc.charset != 'undefined') {
try {
doc.charset = "utf-8"
} catch (exception) {}
}
// appending script to the iframe
var loadScript = function(srcArray) {
if (srcArray.length === 0) {
return;
}
var src = '';
// test if the script has an scr attribute
if (/<\s*script[^>]+src\s*=\s*"([^"]+)"[^>]*>\s*<\/script[^>]*>/gi.test(srcArray[0])) {
var srcIndex = srcArray[0].indexOf('src="') +5;
var lastI = srcArray[0].indexOf('"', srcIndex)
for (var i = srcIndex; i < lastI; i++) {
src += srcArray[0][i]
}
//no src attribute
} else {
var stripedScript = srcArray[0];
stripedScript = stripedScript.replace(/<\s*script[^>]*>/gi, '');
stripedScript = stripedScript.replace('</script>', '');
var newScript = doc.createElement('script');
newScript.setAttribute("type", "text/javascript");
newScript.setAttribute("charset", 'utf8');
newScript.text = stripedScript;
doc.getElementsByTagName('head')[0].appendChild(newScript);
srcArray.shift();
loadScript(srcArray);
return;
}
// has scr attribute
var newScript = doc.createElement('script');
newScript.setAttribute("type", "text/javascript");
newScript.setAttribute("charset", 'utf8');
newScript.setAttribute("src", src);
var head = doc.getElementsByTagName('head')[0];
newScript.onload = newScript.onreadystatechange = function () {
if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
// Handle memory leak in IE
newScript.onload = newScript.onreadystatechange = null;
if (newScript.parentNode) {
head.removeChild(newScript);
}
srcArray.shift();
loadScript(srcArray);
}
};
head.appendChild(newScript);
};
// the scripts array containts the script elements with and without src attribute
loadScript(scripts);
doc.close();