我可以在chrome中获取iframe元素
我正在尝试使用contentwindow.document(或contentDocument)中的iFrame中获取一个元素。
我能够获得有关Firefox和Chrome 99版或更早版本的信息,但突然间,我无法获得100或更晚的Chrome版本的信息。
另外,我不确定它是否相关,但是这种现象仅在WordPress的固定页面上描述的JS中发生,并且没有在HTML页面中发生,而不是WordPress。
来源的轮廓如下。
<iframe src="/form/terms/sample.html" width="100%" height="500px" frameborder="0" id="iframe"></iframe>
<script>
window.onload = function(){
var iframe = document.getElementById("iframe");
var iframe_doc = iframe.contentDocument || iframe.contentWindow.document;
console.log(iframe_doc);
}
</script>
结果在下面,身体元素是空的。
<html>
<head></head>
<body></body>
</html>
WordPress的版本为5.7.6。
你知道原因吗?
I'm trying to get an element in an iframe with contentWindow.document (or contentDocument).
I was able to get information without problems with FireFox and Chrome version 99 or earlier, but suddenly I can not get information with Chrome version 100 or later.
Also, I'm not sure if it's related, but this phenomenon occurred only in the JS described on the fixed page of Wordpress, and did not occur in the html page that is not Wordpress.
The outline of the source is as follows.
<iframe src="/form/terms/sample.html" width="100%" height="500px" frameborder="0" id="iframe"></iframe>
<script>
window.onload = function(){
var iframe = document.getElementById("iframe");
var iframe_doc = iframe.contentDocument || iframe.contentWindow.document;
console.log(iframe_doc);
}
</script>
The result is below, the body element is empty.
<html>
<head></head>
<body></body>
</html>
The version of Wordpress is 5.7.6.
Do you know the cause?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Firefox V102上进行了测试,有以下错误:
und offult domexception:拒绝访问交叉原始对象上的属性“文档”的权限
如果您在网站上进行处理,则会发生错误。
首先在
&lt; head&gt;
中添加&lt; meta&gt;
标记:然后编辑JavaScript代码:
希望这对您有用。
Tested on Firefox v102, got following error:
Uncaught DOMException: Permission denied to access property "document" on cross-origin object
The err happens if you process it on the website starts load.
First add a
<meta>
tag in<head>
:Then edit the JavaScript code:
Hope this works for you.