在缩短document.domain之后,iframe contentWindow将访问被拒绝的错误。

时间:2023-01-28 15:56:25

I create an IFRAME dynamically in the following way:

我以以下方式动态创建一个IFRAME:

var wrapUpIframe = document.createElement("iframe");
wrapUpIframe.id = 'WrapUpDialog3';
wrapUpIframe.src = 'WrapUpDialog.html';    
document.body.appendChild(wrapUpIframe);

after the dynamic creation my document.domain is being shortened from Servername.dc.com to only dc.com,

在动态创建我的文档之后。域名从Servername.dc.com缩短为只有dc.com,

but when I try to access contentWindow I got an Access is denied error:

但是当我尝试访问contentWindow时,我得到一个访问被拒绝错误:

document.getElementById("WrapUpDialog3").contentWindow.SomeFunction();

Note: When I define the IFRAME statically in the HTML it works fine.
I also tried to change my IFRAME document.domain in the following way:

注意:当我在HTML中静态地定义IFRAME时,它可以正常工作。我还试图修改我的IFRAME文档。域名:

WrapUpDialog3.document.domain = dc.com;

I checked both document.domain and my IFRAME domain and they are both identical.

我检查了两个文档。定义域和IFRAME定义域都是相同的。

What can I do?

我能做什么?

I'm working with IE9.

我使用IE9。

3 个解决方案

#1


9  

First take a look at the correct answer from this post. It looks to me like that could be your issue.

首先看看这篇文章的正确答案。在我看来,这可能是你的问题。

If not that then maybe this quick hack I saw from another post might help.

如果不是这样的话,那么我从另一个帖子上看到的这个快速破解可能会有帮助。

 var frame = $('<iframe>')
.attr('id', 'myIframe')
.addClass('someClass')
.attr('src', 'javascript:(function () {' +'document.open();document.domain=\'myDomain.net\';document.close();' + '})();');
.appendTo($('#someDiv'));

Not sure if this is relevant but I also found this on the web link.

不确定这是否相关,但我也在网络链接上找到了。

OK, so to respond to your comment. The javascript function isn't assigning the source, it's setting the document domain which apparently doesn't get done correctly in I.E.

好,那就回复你的评论。javascript函数并没有赋值源,而是设置了文档域,显然在这里不能正确完成

Check out this link for another example and explanation.

请查看此链接以获得另一个示例和解释。

So what I would try might be something like this ...

所以我会试着做这样的事情…

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';}

You might have to play around with how to return the actual url for the iframe after running the function that sets the document domain. But from what I am seeing this might work for you.

在运行设置文档域的函数之后,您可能需要考虑如何返回iframe的实际url。但从我看到的情况来看,这可能对你有用。

I had a similar issue but not exactly the same problem which is why I can't give you an exact fix. The function setting the document domain was what got me past the access denied error.

我有一个相似的问题,但不是完全相同的问题,这就是为什么我不能给你一个确切的解决办法。设置文档域的函数使我通过了访问拒绝错误。

You could also add this to your main document to really make sure the domains match.

您还可以将其添加到主文档中,以确保域匹配。

 <script type="text/javascript">
    document.domain = 'dc.com';
  </script>

I also wanted to add a link for some explanation on explicitly setting the document.domain that I had used before. This was helpful to me in the past. Particularly this quote ...

我还想在显式设置文档的过程中添加一个链接。我以前用过的域名。这在过去对我很有帮助。尤其是这句话……

Explicitly setting the value indicates intent to "cooperate" with a script on another subdomain (under the same parent domain).

显式设置值表示意图与另一个子域上的脚本“协作”(在相同的父域下)。

Dor, you may be having a timing issue. I found some code (here) that I just tested that works for me. It makes sure that the iframe is loaded before you try to access the contentWindow.

多,你可能有时间问题。我在这里找到了一些我刚刚测试过的代码。它确保在尝试访问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;

#2


2  

How do you serve your files? Do you see file:/// in your address bar? If so, try serving your code using a webserver.

你的文件是如何送达的?你在地址栏里看到文件了吗?如果是,尝试使用webserver服务您的代码。

Google Chrome gives an access denied error if I try your code using file:///, but it works when served from a local webserver (i.e. the address starts with http://localhost/).

如果我使用file:///尝试您的代码,谷歌Chrome会给出一个拒绝访问错误,但是当从本地webserver(即以http://localhost/开头的地址)提供服务时,它会工作。

#3


1  

Since you have not yet accepted any of the answers given you probably still have the problem.

既然你还没有接受任何给出的答案,你可能仍然有这个问题。

Try setting document.domain explicitly in both HTML pages (you seem to be doing this in one page only). This means that as @Vic suggested, you need to add the following javascript code to the HTML that includes the iframe:

尝试设置文档。在两个HTML页面中显式地显示域(您似乎只在一个页面中执行此操作)。这意味着正如@Vic所建议的,您需要向包含iframe的HTML添加以下javascript代码:

document.domain = 'dc.com';

This means that your code would look like this:

这意味着您的代码将如下所示:

document.domain = 'dc.com';
var wrapUpIframe = document.createElement("iframe");
wrapUpIframe.id = 'WrapUpDialog3';
wrapUpIframe.src = 'WrapUpDialog.html';    
document.body.appendChild(wrapUpIframe);

Then, in WrapUpDialog.html itself (not in the main page because then you would circumvent the security system!) you need to set document.domain as well:

然后,在WrapUpDialog。html本身(不在主页上,因为你会绕过安全系统!)你需要设置文档。域:

document.domain = dc.com;

So this line of yours will NOT work:

所以你的这行不行:

WrapUpDialog3.document.domain = 'dc.com';

because WrapUpDialog.html itself needs to grant permission to its "parent" page to execute its javascript.

因为WrapUpDialog。html本身需要授予其“父”页面执行其javascript的权限。

There is more info on this page: What does document.domain = document.domain do?.

这一页有更多的信息:什么是文件。域=文档。域做什么?。

Final hint: do try your code using different browsers: IE9, Firefox, Google Chrome. This can help you identify whether you are perhaps dealing with a quirk of one particular browser.

最后提示:一定要使用不同的浏览器来尝试代码:IE9, Firefox,谷歌Chrome。这可以帮助您识别您是否正在处理一个特定浏览器的怪癖。

#1


9  

First take a look at the correct answer from this post. It looks to me like that could be your issue.

首先看看这篇文章的正确答案。在我看来,这可能是你的问题。

If not that then maybe this quick hack I saw from another post might help.

如果不是这样的话,那么我从另一个帖子上看到的这个快速破解可能会有帮助。

 var frame = $('<iframe>')
.attr('id', 'myIframe')
.addClass('someClass')
.attr('src', 'javascript:(function () {' +'document.open();document.domain=\'myDomain.net\';document.close();' + '})();');
.appendTo($('#someDiv'));

Not sure if this is relevant but I also found this on the web link.

不确定这是否相关,但我也在网络链接上找到了。

OK, so to respond to your comment. The javascript function isn't assigning the source, it's setting the document domain which apparently doesn't get done correctly in I.E.

好,那就回复你的评论。javascript函数并没有赋值源,而是设置了文档域,显然在这里不能正确完成

Check out this link for another example and explanation.

请查看此链接以获得另一个示例和解释。

So what I would try might be something like this ...

所以我会试着做这样的事情…

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';}

You might have to play around with how to return the actual url for the iframe after running the function that sets the document domain. But from what I am seeing this might work for you.

在运行设置文档域的函数之后,您可能需要考虑如何返回iframe的实际url。但从我看到的情况来看,这可能对你有用。

I had a similar issue but not exactly the same problem which is why I can't give you an exact fix. The function setting the document domain was what got me past the access denied error.

我有一个相似的问题,但不是完全相同的问题,这就是为什么我不能给你一个确切的解决办法。设置文档域的函数使我通过了访问拒绝错误。

You could also add this to your main document to really make sure the domains match.

您还可以将其添加到主文档中,以确保域匹配。

 <script type="text/javascript">
    document.domain = 'dc.com';
  </script>

I also wanted to add a link for some explanation on explicitly setting the document.domain that I had used before. This was helpful to me in the past. Particularly this quote ...

我还想在显式设置文档的过程中添加一个链接。我以前用过的域名。这在过去对我很有帮助。尤其是这句话……

Explicitly setting the value indicates intent to "cooperate" with a script on another subdomain (under the same parent domain).

显式设置值表示意图与另一个子域上的脚本“协作”(在相同的父域下)。

Dor, you may be having a timing issue. I found some code (here) that I just tested that works for me. It makes sure that the iframe is loaded before you try to access the contentWindow.

多,你可能有时间问题。我在这里找到了一些我刚刚测试过的代码。它确保在尝试访问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;

#2


2  

How do you serve your files? Do you see file:/// in your address bar? If so, try serving your code using a webserver.

你的文件是如何送达的?你在地址栏里看到文件了吗?如果是,尝试使用webserver服务您的代码。

Google Chrome gives an access denied error if I try your code using file:///, but it works when served from a local webserver (i.e. the address starts with http://localhost/).

如果我使用file:///尝试您的代码,谷歌Chrome会给出一个拒绝访问错误,但是当从本地webserver(即以http://localhost/开头的地址)提供服务时,它会工作。

#3


1  

Since you have not yet accepted any of the answers given you probably still have the problem.

既然你还没有接受任何给出的答案,你可能仍然有这个问题。

Try setting document.domain explicitly in both HTML pages (you seem to be doing this in one page only). This means that as @Vic suggested, you need to add the following javascript code to the HTML that includes the iframe:

尝试设置文档。在两个HTML页面中显式地显示域(您似乎只在一个页面中执行此操作)。这意味着正如@Vic所建议的,您需要向包含iframe的HTML添加以下javascript代码:

document.domain = 'dc.com';

This means that your code would look like this:

这意味着您的代码将如下所示:

document.domain = 'dc.com';
var wrapUpIframe = document.createElement("iframe");
wrapUpIframe.id = 'WrapUpDialog3';
wrapUpIframe.src = 'WrapUpDialog.html';    
document.body.appendChild(wrapUpIframe);

Then, in WrapUpDialog.html itself (not in the main page because then you would circumvent the security system!) you need to set document.domain as well:

然后,在WrapUpDialog。html本身(不在主页上,因为你会绕过安全系统!)你需要设置文档。域:

document.domain = dc.com;

So this line of yours will NOT work:

所以你的这行不行:

WrapUpDialog3.document.domain = 'dc.com';

because WrapUpDialog.html itself needs to grant permission to its "parent" page to execute its javascript.

因为WrapUpDialog。html本身需要授予其“父”页面执行其javascript的权限。

There is more info on this page: What does document.domain = document.domain do?.

这一页有更多的信息:什么是文件。域=文档。域做什么?。

Final hint: do try your code using different browsers: IE9, Firefox, Google Chrome. This can help you identify whether you are perhaps dealing with a quirk of one particular browser.

最后提示:一定要使用不同的浏览器来尝试代码:IE9, Firefox,谷歌Chrome。这可以帮助您识别您是否正在处理一个特定浏览器的怪癖。