在 Chrome 中,无法访问子 iframe 或 iframe 父级的任何变量或函数

发布于 2024-10-31 10:37:10 字数 2385 浏览 1 评论 0原文

这与这个问题类似,但范围更广一些。

我只是在本地打开这些页面,它们位于同一个文件夹中。

index.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
    <head>
        <title>TestIndex</title>
        <script type="text/javascript">
            function init()
            {
                alert("child.childvar: " + child.childvar); //works in FF, IE, not Chrome
                alert("frames['child'].childvar: " + frames['child'].childvar); //works in FF, IE, not Chrome
                alert("document.getElementById('child').contentWindow['childvar']: " + document.getElementById('child').contentWindow['childvar']); //works in FF, IE, not Chrome

                child.childfunc(); //works in FF, IE, not Chrome
                frames['child'].childfunc(); //works in FF, IE, not Chrome
                document.getElementById('child').contentWindow['childfunc()']; //doesn't work in anything
            }

            var parentvar = 7;
            function parentfunc()
            {
                alert("In parentfunc");
            }
            window.onload = init;
        </script>
    </head>

    <body>
        <iframe id="child" name="child" src="child.html">Your browser does not support iframes</iframe>
    </body>
</html>

child.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
    <head>
        <title>TestChild</title>
        <script type="text/javascript">
            function init()
            {
                alert("parent.parentvar: " + parent.parentvar); //works in FF, IE, not Chrome

                parent.parentfunc(); //works in FF, IE, not Chrome
            }

            var childvar = 5;
            function childfunc()
            {
                alert("In childfunc");
            }
            window.onload = init;
        </script>
    </head>

<body>

</body>
</html>

我似乎无法在 Chrome 中的页面与其 iframe 内容之间实现任何通信。我确实阅读了我链接到的问题的答案,但我真的不知道用户脚本/内容脚本是什么,所以我不知道这些问题与我的相关性如何。

我想我真正的问题是:我到底如何从 iframe 页面获取值到父页面!?

This is similar to this question, although a bit broader.

I'm just opening these pages locally, and they sit in the same folder.

index.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
    <head>
        <title>TestIndex</title>
        <script type="text/javascript">
            function init()
            {
                alert("child.childvar: " + child.childvar); //works in FF, IE, not Chrome
                alert("frames['child'].childvar: " + frames['child'].childvar); //works in FF, IE, not Chrome
                alert("document.getElementById('child').contentWindow['childvar']: " + document.getElementById('child').contentWindow['childvar']); //works in FF, IE, not Chrome

                child.childfunc(); //works in FF, IE, not Chrome
                frames['child'].childfunc(); //works in FF, IE, not Chrome
                document.getElementById('child').contentWindow['childfunc()']; //doesn't work in anything
            }

            var parentvar = 7;
            function parentfunc()
            {
                alert("In parentfunc");
            }
            window.onload = init;
        </script>
    </head>

    <body>
        <iframe id="child" name="child" src="child.html">Your browser does not support iframes</iframe>
    </body>
</html>

child.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
    <head>
        <title>TestChild</title>
        <script type="text/javascript">
            function init()
            {
                alert("parent.parentvar: " + parent.parentvar); //works in FF, IE, not Chrome

                parent.parentfunc(); //works in FF, IE, not Chrome
            }

            var childvar = 5;
            function childfunc()
            {
                alert("In childfunc");
            }
            window.onload = init;
        </script>
    </head>

<body>

</body>
</html>

I can't seem to achieve any communication at all between a page and its iframe's content in chrome. I did read the answers to the question I linked to, but I don't really know what userscripts/content scripts are, so I don't know how relevant that questions was to mine.

I guess my actual question is: how the hell do I get values from an iframe'd page into the parent page!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

慕烟庭风 2024-11-07 10:37:10

显然,在本地文件系统上,javascript 帧间通信不起作用。将文件放在服务器上,它可能会按预期运行。

Apparently javascript inter-frame communication doesn't work when on the local file system. Put the files on a server and it will likely function as expected.

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