详解html5 postMessage解决跨域通信的问题
本文介绍了详解html5 postMessage解决跨域通信的问题,分享给大家,具体如下:
效果图

postmessage解析HTML5提供了新型机制PostMessage实现安全的跨源通信. 语法 otherWindow.postMessage(message, targetOrigin, [transfer]);
otherWindow: 其他窗口的一个引用, 比如IFRAME的contentWindow属性, 执行,window.open返回的窗口对象. message: 将要发送到其他窗口的数据. targetOrigin:通过窗口的origin属性来指定哪些窗口能接收到消息事件, 其值可以是字符”*”(表示无限制)或者一个URL transfer:是一串和message同时传递的Transferable对象. 这些对象的所有权将被转移给消息的接收方, 而发送一放将不再保有所有权.element.addEventListener(event,fn,useCaption ); 三个参数 event 事件 比如click mouseenter mouseleave 回调函数 useCaption用于描述是冒泡还是捕获。默认值是false,即冒泡传递。 当值为true,就是捕获传递。实现方式
主界面 main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>跨域数据访问</title>
<script type="text/javascript">
window.addEventListener('message',function(e){
console.log("e--->",e);
const data = e.data;
document.getElementById('main1').style.backgroundColor=e.data;
},false)
</script>
</head>
<body>
<div id="main1" style="width:200px;height:200px;margin:100px;border:solid 1px #000;">
我是主界面,等待接收iframe的传递
</div>
<div style="margin:100px;">
iframe
<iframe src="http://localhost:3000/iframe.html" width="800px" height="300px" ></iframe>
</div>
</body>
</html>
iframe界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
html,body{
height:100%;
margin:0px;
}
</style>
</head>
<body style="height:100%;">
<div id="frame" style="height:200px; width:200px;background-color:rgb(204, 204, 0)" onclick="changeColor()">
点击改变颜色
</div>
<script type="text/javascript">
function changeColor(){
var frame = document.getElementById('frame');
var color=frame.style.backgroundColor;
if(color=='rgb(204, 102, 0)'){
color='rgb(204, 204, 0)';
}else{
color='rgb(204,102,0)';
}
console.log("frame===>",frame);
console.log("color",color);
frame.style.backgroundColor=color;
window.parent.postMessage(color,'*');
}
</script>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
html5 postMessage前端跨域并前端监听的方法示例
这篇文章主要介绍了html5 postMessage前端跨域并前端监听的方法示例的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-11-01- 这篇文章主要介绍了html5通过postMessage进行跨域通信的方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-04
- 这篇文章主要介绍了详解HTML5 window.postMessage与跨域,非常具有实用价值,需要的朋友可以参考下2017-05-11
html5 postMessage解决跨域、跨窗口消息传递方案
本篇文章主要介绍了html5 postMessage解决跨域、跨窗口消息传递方案,具有一定的参考价值,有需要的可以了解一下、2016-12-20HTML5中使用postMessage实现两个网页间传递数据
这篇文章主要为大家详细介绍了利用HTML5里的window.postMessage在两个网页间传递数据的相关资料,postMessage API的功能是可以让你在两个浏览器窗口或iframe之间传递信息数2016-06-22- window.postMessage经常被人们利用来做跨域数据传递,下面将为大家来介绍HTML5中的postMessage API基本使用教程,需要的朋友可以参考下2016-05-20
HTML5中使用postMessage实现Ajax跨域请求的方法
这篇文章主要介绍了HTML5中使用postMessage实现Ajax跨域请求的方法的相关资料,需要的朋友可以参考下2016-04-19- 这篇文章主要介绍了Html5 postMessage实现跨域消息传递的相关资料,需要的朋友可以参考下2016-03-11
- 本文是对html5跨域通讯之postMessage的用法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助2013-11-07
HTML5提出了一个新的用来跨域传值的方法,即postMessage,这篇文章主要介绍了HTML5的postMessage的使用手册的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参2018-12-19



最新评论