詳解html5 postMessage解決跨域通信的問題

本文介紹了詳解html5 postMessage解決跨域通信的問題,分享給大家,具體如下:
效果圖
postmessage解析HTML5提供了新型機(jī)制PostMessage實(shí)現(xiàn)安全的跨源通信. 語法 otherWindow.postMessage(message, targetOrigin, [transfer]);
otherWindow: 其他窗口的一個(gè)引用, 比如IFRAME的contentWindow屬性, 執(zhí)行,window.open返回的窗口對象. message: 將要發(fā)送到其他窗口的數(shù)據(jù). targetOrigin:通過窗口的origin屬性來指定哪些窗口能接收到消息事件, 其值可以是字符”*”(表示無限制)或者一個(gè)URL transfer:是一串和message同時(shí)傳遞的Transferable對象. 這些對象的所有權(quán)將被轉(zhuǎn)移給消息的接收方, 而發(fā)送一放將不再保有所有權(quán).element.addEventListener(event,fn,useCaption ); 三個(gè)參數(shù) event 事件 比如click mouseenter mouseleave 回調(diào)函數(shù) useCaption用于描述是冒泡還是捕獲。默認(rèn)值是false,即冒泡傳遞。 當(dāng)值為true,就是捕獲傳遞。實(shí)現(xiàn)方式
主界面 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>跨域數(shù)據(jù)訪問</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()"> 點(diǎn)擊改變顏色 </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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
html5 postMessage前端跨域并前端監(jiān)聽的方法示例
這篇文章主要介紹了html5 postMessage前端跨域并前端監(jiān)聽的方法示例的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-01html5通過postMessage進(jìn)行跨域通信的方法
這篇文章主要介紹了html5通過postMessage進(jìn)行跨域通信的方法的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-04- 這篇文章主要介紹了詳解HTML5 window.postMessage與跨域,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-11
html5 postMessage解決跨域、跨窗口消息傳遞方案
本篇文章主要介紹了html5 postMessage解決跨域、跨窗口消息傳遞方案,具有一定的參考價(jià)值,有需要的可以了解一下、2016-12-20HTML5中使用postMessage實(shí)現(xiàn)兩個(gè)網(wǎng)頁間傳遞數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了利用HTML5里的window.postMessage在兩個(gè)網(wǎng)頁間傳遞數(shù)據(jù)的相關(guān)資料,postMessage API的功能是可以讓你在兩個(gè)瀏覽器窗口或iframe之間傳遞信息數(shù)2016-06-22- window.postMessage經(jīng)常被人們利用來做跨域數(shù)據(jù)傳遞,下面將為大家來介紹HTML5中的postMessage API基本使用教程,需要的朋友可以參考下2016-05-20
HTML5中使用postMessage實(shí)現(xiàn)Ajax跨域請求的方法
這篇文章主要介紹了HTML5中使用postMessage實(shí)現(xiàn)Ajax跨域請求的方法的相關(guān)資料,需要的朋友可以參考下2016-04-19Html5 postMessage實(shí)現(xiàn)跨域消息傳遞
這篇文章主要介紹了Html5 postMessage實(shí)現(xiàn)跨域消息傳遞的相關(guān)資料,需要的朋友可以參考下2016-03-11html5跨域通訊之postMessage的用法總結(jié)
本文是對html5跨域通訊之postMessage的用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-07- HTML5提出了一個(gè)新的用來跨域傳值的方法,即postMessage,這篇文章主要介紹了HTML5的postMessage的使用手冊的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參2018-12-19