Javascript iframe中的不同作用域
我需要他的代码来处理IE8中的iframes+ 我有一个字符串中的html代码,但是在IE8中,带和不带src-s的脚本在不同的范围内。 因此,1。文件是Jquery,但文件名是4。文件运行时出现错误:“未定义jQuery”。 例如:Javascript iframe中的不同作用域,javascript,iframe,internet-explorer-8,Javascript,Iframe,Internet Explorer 8,我需要他的代码来处理IE8中的iframes+ 我有一个字符串中的html代码,但是在IE8中,带和不带src-s的脚本在不同的范围内。 因此,1。文件是Jquery,但文件名是4。文件运行时出现错误:“未定义jQuery”。 例如: <script>jQuery.noConflict();</script> jQuery.noConflict(); 我的代码的重要部分: var iframe = document.getElementById('my_ifr
<script>jQuery.noConflict();</script>
jQuery.noConflict();
我的代码的重要部分:
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();
var-iframe=document.getElementById('my_-iframe'))
if(frame==null){
回来
}
iframe.style.display='block';
var doc=null;
if(iframe.contentDocument)
doc=iframe.contentDocument;
//IE8!
else if(iframe.contentWindow)
doc=iframe.contentWindow.document;
如果(doc==null){
回来
}
//content变量将html放在一个字符串中,这里我从这个字符串中获取脚本元素
//...
doc.open();
文件编写(内容);
if(doc.charset的类型!=“未定义”){
试一试{
doc.charset=“utf-8”
}捕获(异常){}
}
//将脚本追加到iframe
var loadScript=函数(srcArray){
if(srcArray.length==0){
回来
}
var src='';
//测试脚本是否具有scr属性
如果(/]+src\s*=\s*”([^“]+)“[^>]*>\s*]*>/gi.test(srcArray[0])){
var srcIndex=srcArray[0].indexOf('src=“”)+5;
var lastI=srcArray[0]。indexOf(“”,srcIndex)
对于(var i=srcIndex;i/gi',);
stripedScript=stripedScript.replace(“”,”);
var newScript=doc.createElement('script');
setAttribute(“type”、“text/javascript”);
setAttribute(“字符集”,“utf8”);
newScript.text=stripedScript;
doc.getElementsByTagName('head')[0].appendChild(newScript);
srcArray.shift();
加载脚本(srcArray);
回来
}
//具有scr属性
var newScript=doc.createElement('script');
setAttribute(“type”、“text/javascript”);
setAttribute(“字符集”,“utf8”);
setAttribute(“src”,src);
var head=doc.getElementsByTagName('head')[0];
newScript.onload=newScript.onreadystatechange=函数(){
如果(!this.readyState | | this.readyState==“已加载”| | this.readyState==“已完成”){
//处理IE中的内存泄漏
newScript.onload=newScript.onreadystatechange=null;
if(newScript.parentNode){
head.removeChild(newScript);
}
srcArray.shift();
加载脚本(srcArray);
}
};
head.appendChild(newScript);
};
//脚本数组包含带和不带src属性的脚本元素
加载脚本(脚本);
doc.close();
您可能需要在每个外部脚本src中运行$(function(){/*put code here*/})
,看看会发生什么。我不能这样做,因为我有很多这样的脚本,我不能像bootstrap那样重写代码。或者您的意思是在我的代码中?但首先我没有jQuery。