首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 ><iframe> javascript跨域访问父DOM?

<iframe> javascript跨域访问父DOM?
EN

Stack Overflow用户
提问于 2009-08-18 04:28:53
回答 5查看 68.7K关注 0票数 16

我控制嵌入在另一个域的页面中的iframe的内容。有没有办法让我的iframe中的javascript修改父级的DOM?

例如,我想让我的iframed脚本向父DOM添加一组html元素。这似乎是一个相当高的要求--你觉得呢?

编辑:存在一种称为"Fragment ID Messaging“的技术,它可能是一种在跨域iframes之间进行通信的方法。

编辑:还有,Firefox3.5,Opera,Chrome (等)似乎都在采用html5 "postMessage" api,它允许在frames,iframes和popups之间进行安全的跨域数据传输。它的工作方式就像一个事件系统。显然,IE8支持这一特性,这可能有点令人惊讶。

摘要:否,您不能直接访问/编辑来自其他域的页面的DOM。但你可以与它交流,它可以合作做出你想要的改变。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-08-18 04:36:01

我不想这么说,但我有99%的把握,这不是因为安全问题。

你可以在here上试试。

bhh

票数 21
EN

Stack Overflow用户

发布于 2010-11-23 08:28:12

这是可能的。

您在编辑中提到postMessage是正确的。对于那些正在寻找的人来说,有一种很好的向后兼容的、仅限javascript的跨域通信方式。简短,简单的代码也是如此。完美的解决方案?只要您可以请求修改父项和子项:

http://www.onlineaspect.com/2010/01/15/backwards-compatible-postmessage/

票数 11
EN

Stack Overflow用户

发布于 2013-05-31 19:25:09

是的你可以。

您可以实现window.postMessage来跨域使用iframe和/或windows进行通信。

但是你需要以一种异步的方式来做。

如果您需要同步地使用它,则需要在这些异步方法周围实现包装器。

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>

    <!--
    <link rel="shortcut icon" href="/favicon.ico">


    <link rel="start" href="http://benalman.com/" title="Home">

    <link rel="stylesheet" type="text/css" href="/code/php/multi_file.php?m=benalman_css">

    <script type="text/javascript" src="/js/mt.js"></script>
    -->
    <script type="text/javascript">
        // What browsers support the window.postMessage call now?
        // IE8 does not allow postMessage across windows/tabs
        // FF3+, IE8+, Chrome, Safari(5?), Opera10+

        function SendMessage()
        {
            var win = document.getElementById("ifrmChild").contentWindow;

            // http://robertnyman.com/2010/03/18/postmessage-in-html5-to-send-messages-between-windows-and-iframes/


            // http://stackoverflow.com/questions/16072902/dom-exception-12-for-window-postmessage
            // Specify origin. Should be a domain or a wildcard "*"

            if (win == null || !window['postMessage'])
                alert("oh crap");
            else
                win.postMessage("hello", "*");
            //alert("lol");
        }



        function ReceiveMessage(evt) {
            var message;
            //if (evt.origin !== "http://robertnyman.com")
            if (false) {
                message = 'You ("' + evt.origin + '") are not worthy';
            }
            else {
                message = 'I got "' + evt.data + '" from "' + evt.origin + '"';
            }

            var ta = document.getElementById("taRecvMessage");
            if (ta == null)
                alert(message);
            else
                document.getElementById("taRecvMessage").innerHTML = message;

            //evt.source.postMessage("thanks, got it ;)", event.origin);
        } // End Function ReceiveMessage




        if (!window['postMessage'])
            alert("oh crap");
        else {
            if (window.addEventListener) {
                //alert("standards-compliant");
                // For standards-compliant web browsers (ie9+)
                window.addEventListener("message", ReceiveMessage, false);
            }
            else {
                //alert("not standards-compliant (ie8)");
                window.attachEvent("onmessage", ReceiveMessage);
            }
        }
    </script>


</head>
<body>

    <iframe id="ifrmChild" src="child.htm" frameborder="0" width="500" height="200" ></iframe>
    <br />


    <input type="button" value="Test" onclick="SendMessage();" />

</body>
</html>

Child.htm

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>

    <!--
    <link rel="shortcut icon" href="/favicon.ico">


    <link rel="start" href="http://benalman.com/" title="Home">

    <link rel="stylesheet" type="text/css" href="/code/php/multi_file.php?m=benalman_css">

    <script type="text/javascript" src="/js/mt.js"></script>
    -->

    <script type="text/javascript">
        /*
        // Opera 9 supports document.postMessage() 
        // document is wrong
        window.addEventListener("message", function (e) {
            //document.getElementById("test").textContent = ;
            alert(
                e.domain + " said: " + e.data
                );
        }, false);
        */

        // https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage
        // http://ejohn.org/blog/cross-window-messaging/
        // http://benalman.com/projects/jquery-postmessage-plugin/
        // http://benalman.com/code/projects/jquery-postmessage/docs/files/jquery-ba-postmessage-js.html

        // .data – A string holding the message passed from the other window.
        // .domain (origin?) – The domain name of the window that sent the message.
        // .uri – The full URI for the window that sent the message.
        // .source – A reference to the window object of the window that sent the message.
        function ReceiveMessage(evt) {
            var message;
            //if (evt.origin !== "http://robertnyman.com")
            if(false)
            {
                message = 'You ("' + evt.origin + '") are not worthy';
            }
            else
            {
                message = 'I got "' + evt.data + '" from "' + evt.origin + '"';
            }

            //alert(evt.source.location.href)

            var ta = document.getElementById("taRecvMessage");
            if(ta == null)
                alert(message);
            else
                document.getElementById("taRecvMessage").innerHTML = message;

            // http://javascript.info/tutorial/cross-window-messaging-with-postmessage
            //evt.source.postMessage("thanks, got it", evt.origin);
            evt.source.postMessage("thanks, got it", "*");
        } // End Function ReceiveMessage




        if (!window['postMessage'])
            alert("oh crap");
        else {
            if (window.addEventListener) {
                //alert("standards-compliant");
                // For standards-compliant web browsers (ie9+)
                window.addEventListener("message", ReceiveMessage, false);
            }
            else {
                //alert("not standards-compliant (ie8)");
                window.attachEvent("onmessage", ReceiveMessage);
            }
        }
    </script>


</head>
<body style="background-color: gray;">
    <h1>Test</h1>

    <textarea id="taRecvMessage" rows="20" cols="20" ></textarea>

</body>
</html>

在这里,您将修改子节点以向父节点发送postmessages消息。例如,在child.htm中,您需要

代码语言:javascript
复制
window.parent.postMessage("alert(document.location.href); document.location.href = 'http://www.google.com/ncr'", "*");

在parent中,您(在receiveMessage中) eval(evt.data);并不是说使用eval是不安全的,所以您将改为传递一个枚举,并调用需要放在父页面上的相应函数。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1291812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档