码迷,mamicode.com
首页 > 其他好文 > 详细

H5中postMessage解决数据跨域

时间:2017-11-13 13:35:53      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:传递   script   添加   解决   style   100%   key   ext   logs   

一、发送端(http://a.domain.com):

1、添加一个iframe,指定接收的页面:

<iframe id="receivePage" src="http://b.domain.com/receive.html"></iframe>

2、在js中添加postMessage方法:

document.getElementById("receivePage").contentWindow.postMessage("要传递的数据","*");

二、接收端(http://b.domain.com)的receive.html:

<!doctype html>
<html>
<head>
</head>
<body style="height:100%;">
    <script type="text/javascript">
            window.addEventListener(message, function(e) {
                if (e.source != window.parent)
                    return;
                if(e.origin != "http://a.domain.com")//请求源验证
                    return;
                localStorage.YourKey = e.data;
            }, false);
    </script>
</body>
</html>

 

H5中postMessage解决数据跨域

标签:传递   script   添加   解决   style   100%   key   ext   logs   

原文地址:http://www.cnblogs.com/dances/p/7825604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!