Dor*_*hen 10 javascript iframe internet-explorer dynamic access-denied
我通过以下方式动态创建IFRAME:
var wrapUpIframe = document.createElement("iframe");
wrapUpIframe.id = 'WrapUpDialog3';
wrapUpIframe.src = 'WrapUpDialog.html';
document.body.appendChild(wrapUpIframe);
Run Code Online (Sandbox Code Playgroud)
在动态创作之后,我document.domain被缩短Servername.dc.com到只有dc.com,
但是当我尝试访问时,contentWindow我得到了一个Access被拒绝的错误:
document.getElementById("WrapUpDialog3").contentWindow.SomeFunction();
Run Code Online (Sandbox Code Playgroud)
注意:当我在HTML中静态定义IFRAME时,它可以正常工作.
我还试图document.domain通过以下方式更改我的IFRAME :
WrapUpDialog3.document.domain = dc.com;
Run Code Online (Sandbox Code Playgroud)
我检查了两个document.domain和我的IFRAME域,它们都是相同的.
我能做什么?
我正在使用IE9.
小智 9
首先来看看这篇文章的正确答案.在我看来,这可能是你的问题.
如果不是这样,那么我从另一篇文章中看到的这个快速黑客可能有所帮助.
var frame = $('<iframe>')
.attr('id', 'myIframe')
.addClass('someClass')
.attr('src', 'javascript:(function () {' +'document.open();document.domain=\'myDomain.net\';document.close();' + '})();');
.appendTo($('#someDiv'));
Run Code Online (Sandbox Code Playgroud)
不确定这是否相关,但我也在网络链接上找到了这个.
好的,所以回复你的评论.javascript函数没有分配源,它设置的文档域显然没有在IE中正确完成
查看此链接以获取其他示例和说明.
所以我想尝试的可能是这样的......
var wrapUpIframe = document.createElement("iframe");
wrapUpIframe.id = 'WrapUpDialog3';
wrapUpIframe.src = setSrc();
document.body.appendChild(wrapUpIframe);
function setSrc(){document.open();document.domain=\'dc.com\';document.close();return 'WrapUpDialog.html';}
Run Code Online (Sandbox Code Playgroud)
在运行设置文档域的函数之后,您可能必须使用如何返回iframe的实际URL.但从我所看到的,这可能对你有用.
我有一个类似的问题,但不完全相同的问题,这就是为什么我不能给你一个确切的解决方案.设置文档域的功能是让我超越访问被拒绝的错误.
您还可以将其添加到主文档中,以确保域匹配.
<script type="text/javascript">
document.domain = 'dc.com';
</script>
Run Code Online (Sandbox Code Playgroud)
我还想添加一个链接,以便明确设置我之前使用过的document.domain.这对我来说过去很有帮助.特别是这句话......
显式设置该值表示意图与另一个子域(在同一父域下)上的脚本"合作".
多尔,你可能有时间问题.我找到了一些我刚刚测试过的代码(这里).它确保在您尝试访问contentWindow之前加载iframe.
var iframe = document.createElement("iframe");
iframe.src = "WrapUpDialog.html";
if (iframe.attachEvent){
iframe.attachEvent("onload", function(){
alert("Local iframe is now loaded.");
});
} else {
iframe.onload = function(){
alert("Local iframe is now loaded.");
};
}
document.body.appendChild(iframe);
var iframeWindow = iframe.contentWindow || iframe.contentDocument.parentWindow;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45293 次 |
| 最近记录: |