首页 > 其他 > 详细

H5中postMessage解决数据跨域

时间:2017-11-13 13:16:21      阅读:190      评论:0      收藏:0      [点我收藏+]

一、发送端(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解决数据跨域

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!