admin管理员组文章数量:1327660
When I try to access the iframe, I get this error and I'm not sure what i'm doing wrong. Can someone help me out with this problem?
var ifrm = document.getElementById('iframe'),
ifrm = (ifrm.contentWindow) ? ifrm.contentWindow :
(ifrm.contentDocument.document)
? ifrm.contentDocument.document :
ifrm.contentDocument;
ifrm.open();
ifrm.write("Hello World!");
ifrm.close();
These are the errors I recieve:
Uncaught TypeError: Cannot read property 'document' of undefined
Uncaught TypeError: Cannot read property 'readyState' of undefined
When I try to access the iframe, I get this error and I'm not sure what i'm doing wrong. Can someone help me out with this problem?
var ifrm = document.getElementById('iframe'),
ifrm = (ifrm.contentWindow) ? ifrm.contentWindow :
(ifrm.contentDocument.document)
? ifrm.contentDocument.document :
ifrm.contentDocument;
ifrm.open();
ifrm.write("Hello World!");
ifrm.close();
These are the errors I recieve:
Uncaught TypeError: Cannot read property 'document' of undefined
Uncaught TypeError: Cannot read property 'readyState' of undefined
Share Improve this question edited Feb 16, 2020 at 7:15 sideshowbarker♦ 88.4k29 gold badges215 silver badges212 bronze badges asked Jul 3, 2012 at 18:54 cclervcclerv 2,9698 gold badges37 silver badges43 bronze badges 4- 3 Cannot read property 'document' of undefined is pretty clear – Alex Commented Jul 3, 2012 at 18:56
-
1
iframe.contentWindow
is not cross-browser? – Šime Vidas Commented Jul 3, 2012 at 19:00 - 3 On a side note: Say "No" to nested ternary operators. – FishBasketGordo Commented Jul 3, 2012 at 19:02
-
I assume
ifrm
is a reference to an iframe node (likevar ifrm = document.querySelector('iframe')
for example). In that case you can have:ifrm.contentDocument
orifrm.contentWindow.document
. – ZER0 Commented Jul 3, 2012 at 19:02
2 Answers
Reset to default 3You are looking for the DOM element:
<iframe>
iframe is the name of an HTML tag, not the value of the id of this element (which would be defined as id="value") so you want to use:
document.getElementsByTagName('iframe')[0]
Change the 0 to whatever index is desired if there are multiple iframes on your page as getElementsByTagName() will return an array of results, even if there is only one iframe on the page.
Furthermore, you can simplify your ternary operation as follows:
ifrm = ifrm.contentWindow ? ifrm.contentWindow.document : ifrm.contentDocument;
This way you can obtain the document object for the iframe in all browsers which will give you access to the open(), write(), and close() methods as well as the readyState attribute.
put javascript source after tag.
like this
<html>
<head>
</head>
<body>
<iframe>
<script>
blah blah
</script>
</body>
</html>
本文标签: javascriptAccessing IFrameStack Overflow
版权声明:本文标题:javascript - Accessing IFrame - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742228396a2436757.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论