HTML5的postMessage的使用手冊

我們在碼代碼的時候,經(jīng)常會碰到以下跨域的情況:
1、頁面內(nèi)嵌套iframe,與iframe的消息傳遞
2、頁面與多個頁面之間的傳遞消息
針對這些令人頭疼的跨域問題,html5特地推出新功能--postMessage(跨文檔消息傳輸)。postMessage在使用時,需要傳入2個參數(shù),data和originUrl。data是指需要傳遞的內(nèi)容,但是部分瀏覽器只能處理字符串參數(shù),所以我們一般把data序列化一下,即JSON.stringify(),originUrl是指目標(biāo)url,指定的窗口。
下面直接甩例子,相信大家更容易理解寫。
1、頁面內(nèi)嵌套iframe
父頁面:
html:
<div id='parent'>hello word postMessage</div> <iframe src="http://127.0.0.1:8082/index2.html" id='child'></iframe>
js:
window.onload=function(){ window.frames[0].postMessage('postMessage','http://127.0.0.1:8082/index2.html') } window.addEventListener('message',function(e){ console.log(e) document.getElementById('parent').style.color=e.data })
子頁面:
html:
<div id='button' onclick='changeColor();' style="color:yellow">接受信息</div>
js:
window.addEventListener('message',function(e){ console.log(e) let color = document.getElementById('button').style.color window.parent.postMessage(color,'http://127.0.0.1:8081/index.html') }); function changeColor(){ let buttonColor = document.getElementById('button').style.color buttonColor='#f00' window.parent.postMessage(buttonColor,'http://127.0.0.1:8081/index.html') }
父頁面通過postMessage的方法向iframe傳遞消息,而子頁面通過window.addEventListener監(jiān)聽message方法來獲取到父頁面?zhèn)鬟f的值。如下圖所示,data是父頁面?zhèn)鬟f的值。
子頁面向父頁面?zhèn)鬟f消息,也是通過postMessage的方法去傳遞消息,不是過是以window.parent.postMessage(data,url)的方式傳值。父頁面獲取值也是同樣監(jiān)聽message事件。
2、多頁面之間傳遞消息
父頁面:
html:
<div id='parent' onclick="postMessage()">hello word postMessage</div>
js:
let parent = document.getElementById('parent') function postMessage(){ let windowOpen=window.open('http://127.0.0.1:8082/index2.html','postMessage') setTimeout(function(){ windowOpen.postMessage('postMessageData','http://127.0.0.1:8082/index2.html') },1000) }
子頁面:
html:
<div id='button' onclick='changeColor();' style="color:#f00">接受信息</div>
js:
window.addEventListener('message',function(e){ console.log(e) });
父頁面向子頁面?zhèn)鬟f消息通過window.open打開另一個頁面,然后向他傳值。需要注意的是,使用postMessage傳值的時候需要使用setTimeout去延遲消息的傳遞,因為子頁面的加載不是一下子就加載完成的,也就是說子頁面的監(jiān)聽事件還未開始,此時傳值過去是接收不到的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
html5 postMessage前端跨域并前端監(jiān)聽的方法示例
這篇文章主要介紹了html5 postMessage前端跨域并前端監(jiān)聽的方法示例的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-01- 這篇文章主要介紹了詳解html5 postMessage解決跨域通信的問題的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-17
html5通過postMessage進(jìn)行跨域通信的方法
這篇文章主要介紹了html5通過postMessage進(jìn)行跨域通信的方法的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-04- 這篇文章主要介紹了詳解HTML5 window.postMessage與跨域,非常具有實用價值,需要的朋友可以參考下2017-05-11
html5 postMessage解決跨域、跨窗口消息傳遞方案
本篇文章主要介紹了html5 postMessage解決跨域、跨窗口消息傳遞方案,具有一定的參考價值,有需要的可以了解一下、2016-12-20HTML5中使用postMessage實現(xiàn)兩個網(wǎng)頁間傳遞數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了利用HTML5里的window.postMessage在兩個網(wǎng)頁間傳遞數(shù)據(jù)的相關(guān)資料,postMessage API的功能是可以讓你在兩個瀏覽器窗口或iframe之間傳遞信息數(shù)2016-06-22- window.postMessage經(jīng)常被人們利用來做跨域數(shù)據(jù)傳遞,下面將為大家來介紹HTML5中的postMessage API基本使用教程,需要的朋友可以參考下2016-05-20
HTML5中使用postMessage實現(xiàn)Ajax跨域請求的方法
這篇文章主要介紹了HTML5中使用postMessage實現(xiàn)Ajax跨域請求的方法的相關(guān)資料,需要的朋友可以參考下2016-04-19Html5 postMessage實現(xiàn)跨域消息傳遞
這篇文章主要介紹了Html5 postMessage實現(xiàn)跨域消息傳遞的相關(guān)資料,需要的朋友可以參考下2016-03-11html5跨域通訊之postMessage的用法總結(jié)
本文是對html5跨域通訊之postMessage的用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-07