最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

HTML5的postMessage的使用手冊(cè)

  發(fā)布時(shí)間:2018-12-19 15:41:45   作者:年若芳華   我要評(píng)論
HTML5提出了一個(gè)新的用來(lái)跨域傳值的方法,即postMessage,這篇文章主要介紹了HTML5的postMessage的使用手冊(cè)的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

我們?cè)诖a代碼的時(shí)候,經(jīng)常會(huì)碰到以下跨域的情況:

1、頁(yè)面內(nèi)嵌套iframe,與iframe的消息傳遞

2、頁(yè)面與多個(gè)頁(yè)面之間的傳遞消息

針對(duì)這些令人頭疼的跨域問(wèn)題,html5特地推出新功能--postMessage(跨文檔消息傳輸)。postMessage在使用時(shí),需要傳入2個(gè)參數(shù),data和originUrl。data是指需要傳遞的內(nèi)容,但是部分瀏覽器只能處理字符串參數(shù),所以我們一般把data序列化一下,即JSON.stringify(),originUrl是指目標(biāo)url,指定的窗口。

下面直接甩例子,相信大家更容易理解寫。

1、頁(yè)面內(nèi)嵌套iframe

父頁(yè)面:

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

})

子頁(yè)面:

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')

}

父頁(yè)面通過(guò)postMessage的方法向iframe傳遞消息,而子頁(yè)面通過(guò)window.addEventListener監(jiān)聽(tīng)message方法來(lái)獲取到父頁(yè)面?zhèn)鬟f的值。如下圖所示,data是父頁(yè)面?zhèn)鬟f的值。

子頁(yè)面向父頁(yè)面?zhèn)鬟f消息,也是通過(guò)postMessage的方法去傳遞消息,不是過(guò)是以window.parent.postMessage(data,url)的方式傳值。父頁(yè)面獲取值也是同樣監(jiān)聽(tīng)message事件。

2、多頁(yè)面之間傳遞消息

父頁(yè)面:

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) 

}

子頁(yè)面:

html:

<div id='button' onclick='changeColor();' style="color:#f00">接受信息</div>

js:

window.addEventListener('message',function(e){

      console.log(e)

 });

父頁(yè)面向子頁(yè)面?zhèn)鬟f消息通過(guò)window.open打開(kāi)另一個(gè)頁(yè)面,然后向他傳值。需要注意的是,使用postMessage傳值的時(shí)候需要使用setTimeout去延遲消息的傳遞,因?yàn)樽禹?yè)面的加載不是一下子就加載完成的,也就是說(shuō)子頁(yè)面的監(jiān)聽(tīng)事件還未開(kāi)始,此時(shí)傳值過(guò)去是接收不到的。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

广南县| 上蔡县| 安图县| 水富县| 桦川县| 六安市| 廉江市| 临澧县| 信丰县| 孝义市| 大悟县| 平邑县| 车致| 康定县| 长垣县| 柏乡县| 永靖县| 石景山区| 建阳市| 普宁市| 阜南县| 平定县| 亚东县| 濮阳市| 北海市| 资溪县| 运城市| 普安县| 大同县| 根河市| 绥棱县| 库尔勒市| 梁河县| 浦城县| 安新县| 宝丰县| 柳河县| 石棉县| 茶陵县| 西昌市| 吉林省|