我可以在chrome中获取iframe元素

发布于 2025-02-13 09:15:26 字数 776 浏览 1 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅士风度i 2025-02-20 09:15:26

在Firefox V102上进行了测试,有以下错误:und offult domexception:拒绝访问交叉原始对象上的属性“文档”的权限

如果您在网站上进行处理,则会发生错误。

首先在&lt; head&gt;中添加&lt; meta&gt;标记:

<meta name="referer" content="access-control-allow-origin"/>

然后编辑JavaScript代码:

(function() {
    var iframe = document.getElementById("iframe");
    var iframe_doc = iframe.contentDocument || iframe.contentWindow.document;
    console.log(iframe_doc);
})();

希望这对您有用。

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>:

<meta name="referer" content="access-control-allow-origin"/>

Then edit the JavaScript code:

(function() {
    var iframe = document.getElementById("iframe");
    var iframe_doc = iframe.contentDocument || iframe.contentWindow.document;
    console.log(iframe_doc);
})();

Hope this works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文