【发布时间】:2010-12-25 14:03:18
【问题描述】:
我有一个项目,我需要在其中使用 JavaScript 创建一个
我没有设置
我可以正常工作,但有一个严重的例外 - 如果在父页面中设置了 document.domain 属性(可能是在部署此小部件的某些环境中)、Internet Explorer(可能是所有版本,但我已在 6、7 和 8 中确认)当我尝试访问我创建的此
这是有道理的,因为我知道 Internet Explorer 要求您将所有将相互通信的窗口/框架的 document.domain 设置为相同的值。但是,我不知道有什么方法可以在我无法访问的文档上设置此值。
是否有人知道这样做的方法 - 以某种方式设置这个动态创建的
这是我的测试代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Document.domain Test</title>
<script type="text/javascript">
document.domain = 'onespot.com'; // set the page's document.domain
</script>
</head>
<body>
<p>This is a paragraph above the <iframe>.</p>
<div id="placeholder"></div>
<p>This is a paragraph below the <iframe>.</p>
<script type="text/javascript">
var iframe = document.createElement('iframe'), doc; // create <iframe> element
document.getElementById('placeholder').appendChild(iframe); // append <iframe> element to the placeholder element
setTimeout(function() { // set a timeout to give browsers a chance to recognize the <iframe>
doc = iframe.contentWindow || iframe.contentDocument; // get a handle on the <iframe> document
alert(doc);
if (doc.document) { // HEREIN LIES THE PROBLEM
doc = doc.document;
}
doc.body.innerHTML = '<h1>Hello!</h1>'; // add an element
}, 10);
</script>
</body>
</html>
我将它托管在:
http://troy.onespot.com/static/access_denied.html
如果你在 IE 中加载这个页面,你会看到,在我调用 alert() 时,我确实有一个
非常感谢您的任何帮助或建议!我将感谢任何可以帮助我找到解决方案的人。
【问题讨论】:
-
troy.onespot 的链接已失效。
标签: javascript internet-explorer dom iframe document