访问 IFrame 的 contentWindow.document 会显示“访问被拒绝”;在IE6上
我不认为这是可能的,但我现在对 IE6 的讨厌程度是今天早上的两倍。
请不要打扰我为什么,但我会尽力解释我们想要实现的目标。 我们有 2 个应用程序正在运行,比如说 APP1 和 APP2。 APP2,它们都位于同一域。 APP1 包含托管在 APP2 上的 JavaScript 文件。该 JS 文件将:
- 创建一个 IFrame(使用 document.createElement)
- 将 IFrame 的源设置为 APP2 的根目录(在其中生成一些 HTML);
- 将 div 添加到 APP1 的主体中
- 读取 IFrame 的内容(因此 APP2 生成的 HTML)
- 将此内容设置为 div 的innerHTML (3)
所以最后在 APP1 中我们有一个标头,其中内容由以下内容生成APP2 屏幕上没有 IFrame。
问题出在第4步;这对于除 IE6 之外的所有浏览器都运行良好(这可能是 Web 开发中最常用的句子吗?)。 尝试访问 iframe 的 contentWindow.document 时出现 JS 错误:“访问被拒绝”。我不是专家,但据我了解,如果两个应用程序不在同一个域中,但它们在(dev.echnet/APP1 和 dev.echnet/APP2)上,您会收到此错误。
这是我用于上述步骤的代码(去掉了如果不是 IE6 则不会执行的内容): (1)、(2) & (3):
var elIf;
$().ready(function()
{
elIf = document.createElement('<iframe name="uhframename">');
elIf.setAttribute('id','idUhFrame');
document.body.appendChild(elIf);
var uhDiv = document.createElement('div');
document.body.appendChild(uhDiv);
elIf.src='dev.echnet/APP1?nocache='+Math.random();
getText();
}
(4)
function getText() {
var sContent = "";
if (elIf.contentWindow.document.body) { // access denied on elIf.contentWindow.document
...
}
}
我在谷歌上搜索了很多,并尝试了很多我发现的可能性(也在SO上),但他们似乎都没有解决这个问题。
我还尝试通过将其源设置为以下方式在 IFrame 上显式设置域:
“javascript:(function(){document.open();document.domain=\'dev.echnet\';document.close();})()”
但我不确定这是否有任何影响,因为我正在进一步将源设置为其他内容。无论如何,既然是同一个域,那应该不重要吧?
希望有人帮助或有人击落所有 IE6 用户(现在这是一个可行的任务),这样我就可以跳过这个任务:-)。
I did not think it was possible but I hate IE6 twice as much now then this morning.
Please don't bug me about the why but I'll try to explain what we're trying to achieve.
We have 2 apps running, let's say APP1 & APP2, both of them are on the same domain. APP1 is including a JavaScript file that is hosted on APP2. This JS file will:
- Create an IFrame (using document.createElement)
- Set the source of the IFrame to the root of APP2 (where some HTML is generated);
- Add a div to the body of APP1
- Read the contents of the IFrame (so the generated HTML of APP2)
- set this contents as the innerHTML of the div (3)
So in the end in APP1 we have a header where the contents is generated by APP2 without there being an IFrame on the screen.
The problem lies in step 4; this is working fine for all browsers except for IE6 (could this be the most used sentence in web development?).
I get a JS-error when trying to access contentWindow.document of the iframe: 'Access is denied'. I'm no expert but as I understand this error you would get if both apps were not on the same domain but they are (dev.echnet/APP1 & dev.echnet/APP2).
This is the code I use for above steps (took out stuff that is not executed if not IE6):
(1), (2) & (3):
var elIf;
$().ready(function()
{
elIf = document.createElement('<iframe name="uhframename">');
elIf.setAttribute('id','idUhFrame');
document.body.appendChild(elIf);
var uhDiv = document.createElement('div');
document.body.appendChild(uhDiv);
elIf.src='dev.echnet/APP1?nocache='+Math.random();
getText();
}
(4)
function getText() {
var sContent = "";
if (elIf.contentWindow.document.body) { // access denied on elIf.contentWindow.document
...
}
}
I've googled a lot and tried many possibilities I found (also on SO) but none of them seem to solve this issue.
I also tried setting the domain explicitly on the IFrame by settings its source to this:
"javascript:(function(){document.open();document.domain=\'dev.echnet\';document.close();})()"
but I'm not exactly sure if this has any effect since I'm setting the source to something else a few steps further. Anyway, since it's the same domain it should not matter?
Hoping for some help or someone to shoot down all IE6 users (a feasible task nowadays) so I can skip this task :-).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在对这里的另一个问题进行一些处理之后,我想出了这个解决方案。
似乎
.document
并不总是必要的。http://jsfiddle.net/sTkVR/4/
我正在使用 chrome,但它不是与
.document
一起使用,但没有它也能正常工作After some working with it for another question on here, I've come up with this solution.
Seems
.document
isn't always necessary.http://jsfiddle.net/sTkVR/4/
I'm using chrome, and it was not working with
.document
but works like a charm with out it