use*_*908 58 html javascript iframe document
我有这个HTML代码:
<html>
<head>
<script type="text/javascript">
function GetDoc(x)
{
return x.document ||
x.contentDocument ||
x.contentWindow.document;
}
function DoStuff()
{
var fr = document.all["myframe"];
while(fr.ariaBusy) { }
var doc = GetDoc(fr);
if (doc == document)
alert("Bad");
else
alert("Good");
}
</script>
</head>
<body>
<iframe id="myframe" src="http://example.com" width="100%" height="100%" onload="DoStuff()"></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
问题是我收到消息"Bad".这意味着iframe的文档没有正确获取,GetDoc函数返回的实际内容是父文档.
如果你告诉我哪里弄错了,我会很感激.(我希望在IFrame中托管文档.)
谢谢.
pka*_*ing 126
您应该能够使用以下代码访问IFRAME中的文档:
document.getElementById('myframe').contentWindow.document
Run Code Online (Sandbox Code Playgroud)
但是,如果框架中的页面是从其他域(例如google.com)加载的,则无法执行此操作.这是因为浏览器的同源策略.
Tim*_*own 14
问题是在IE中(我认为你正在测试),该<iframe>元素有一个document属性,该属性引用包含iframe的文档,并且在contentDocumentor或contentWindow.documentproperties 之前使用它.你需要的是:
function GetDoc(x) {
return x.contentDocument || x.contentWindow.document;
}
Run Code Online (Sandbox Code Playgroud)
此外,document.all并非在所有浏览器中都可用且非标准.请document.getElementById()改用.
| 归档时间: |
|
| 查看次数: |
119507 次 |
| 最近记录: |