Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 从IE中的iFrame访问父文档_Jquery_Html_Iframe - Fatal编程技术网

Jquery 从IE中的iFrame访问父文档

Jquery 从IE中的iFrame访问父文档,jquery,html,iframe,Jquery,Html,Iframe,可能重复: 我正在尝试使用$('#u hf_iFrame',top.document)从动态子文档访问父文档。它适用于Firefox、Chrome和Safari,但IE会抛出一个拒绝访问的异常 我正在使用以下代码动态创建 这个问题是这个问题的继续。我使用以下代码将动态添加到文档中 var _hf_iFrame = document.createElement("iframe"); _hf_iFrame.setAttribute("id", "_hf_iFrame"); _hf_iFrame.

可能重复:

我正在尝试使用
$('#u hf_iFrame',top.document)
从动态
子文档访问父文档。它适用于Firefox、Chrome和Safari,但IE会抛出一个拒绝访问的异常

我正在使用以下代码动态创建

这个问题是这个问题的继续。我使用以下代码将动态添加到文档中

var _hf_iFrame = document.createElement("iframe");
_hf_iFrame.setAttribute("id", "_hf_iFrame");
_hf_iFrame.setAttribute("name", "_hf_iFrame");
_hf_iFrame.setAttribute("allow-transparency", true);
_hf_iFrame.setAttribute("style", "height: 354px; width: 445px; border: 0; top: 23%; position: fixed; left:0; overflow: show; background:transparent;");
document.body.appendChild(_hf_iFrame);
_hf_iFrame.setAttribute("src", "javascript:false");

var myContent = '<!DOCTYPE html>'
+ '<html><head><title></title><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script><script type="text/javascript" src="http://somedomain.com/js/core.js"></script></head>'
+ '<body style="margin: 0px;"></body></html>';
_hf_iFrame.contentWindow.document.open('text/html', 'replace');
_hf_iFrame.contentWindow.document.write(myContent);
_hf_iFrame.contentWindow.document.close();
var\u hf\u iFrame=document.createElement(“iFrame”);
_setAttribute(“id”,“\u hf\u iFrame”);
_setAttribute(“名称”,即“iFrame”);
_hf_iFrame.setAttribute(“允许透明”,true);
_hf_iFrame.setAttribute(“样式”,“高度:354px;宽度:445px;边框:0;顶部:23%;位置:固定;左侧:0;溢出:显示;背景:透明;”);
document.body.appendChild(_-hf_-iFrame);
_setAttribute(“src”,“javascript:false”);
var myContent=''
+ ''
+ '';
_hf_iFrame.contentWindow.document.open('text/html','replace');
_hf_iFrame.contentWindow.document.write(myContent);
_hf_iFrame.contentWindow.document.close();

如何解决这个问题?

如果父对象和iframe都在同一个域中,我认为您可以从iframe获取父对象。否则,子iframe或父iframe无法相互访问

设置域

document.domain = 'yourdomain.com';
编辑:


document.domain只能设置为当前域的子集。因此
developer.mozilla.org
可以设置为
mozilla.org
,但不能设置为
mozilla.com
。在某些浏览器中,此属性也可能是只读的

这可能会解决您的问题


document.domain=“yourdomain.com”;
基本上,JS认为即使是子域,如 img.yourdomain.com是与www.yourdomain.com不同的域。 正因为如此,跨这两个子域的页面的AJAX将无法实现 工作另外,如果您有一个从一个到另一个的iframe,您将不会 能够前后引用JS变量或函数


我已将代码更改为

//var source = "javascript:void((function(){document.open();document.domain=\'cloudapp.net\';document.close();})())";  
var source = "javascript:false";
var elem = document.createElement("iframe");
elem.frameBorder = "0";
elem.src = source;
elem.style.width = "100%";
elem.style.margin = "0px";
elem.style.padding = "0px";
elem.setAttribute("id", "_hf_iFrame");
document.body.appendChild(elem);

elem.contentWindow.document.open('text/html', 'replace');
elem.contentWindow.document.write(myContent);
elem.contentWindow.document.close();

而且它可以在所有浏览器上运行。。。但我仍然找不到出现这种奇怪问题的原因。

看起来你想问的问题和这里一样:。在给定的主题上只有一个问题。如果您的另一个问题不够清楚(这似乎是个问题),请编辑它。@jfriend00很抱歉在同一主题上创建了多个问题。。看起来这里可能会有答案:@jfriend00非常感谢您。。。这种联系很有帮助。但是请查看我的回答
文档。域
只能设置为当前域的子集。因此
developer.mozilla.org
可以设置为
mozilla.org
,但不能设置为
mozilla.com
。在某些浏览器中,此属性也可能是只读的。有关详细信息,请参阅。@jfriend00我已在两个文档上设置了
document.domain
,没有任何错误,并且设置了
警报(document.domain)反映了同样的情况。但是IE仍然在
$('#hf_iFrame',top.document)
@jfriend00是的,你是对的,我已经更新了我的答案,thanks@LibinTK查看更新部分…我已更新我的问题。。。我希望现在一切都清楚了。。
//var source = "javascript:void((function(){document.open();document.domain=\'cloudapp.net\';document.close();})())";  
var source = "javascript:false";
var elem = document.createElement("iframe");
elem.frameBorder = "0";
elem.src = source;
elem.style.width = "100%";
elem.style.margin = "0px";
elem.style.padding = "0px";
elem.setAttribute("id", "_hf_iFrame");
document.body.appendChild(elem);

elem.contentWindow.document.open('text/html', 'replace');
elem.contentWindow.document.write(myContent);
elem.contentWindow.document.close();