本篇文章為大家展示了postMessage如何在HTML5中使用,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)、泰寧網(wǎng)絡(luò)推廣、微信小程序定制開發(fā)、泰寧網(wǎng)絡(luò)營銷、泰寧企業(yè)策劃、泰寧品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供泰寧建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:jinyejixie.com
1、頁面內(nèi)嵌套iframe,與iframe的消息傳遞
2、頁面與多個(gè)頁面之間的傳遞消息
針對這些令人頭疼的跨域問題,html5特地推出新功能--postMessage(跨文檔消息傳輸)。postMessage在使用時(shí),需要傳入2個(gè)參數(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打開另一個(gè)頁面,然后向他傳值。需要注意的是,使用postMessage傳值的時(shí)候需要使用setTimeout去延遲消息的傳遞,因?yàn)樽禹撁娴募虞d不是一下子就加載完成的,也就是說子頁面的監(jiān)聽事件還未開始,此時(shí)傳值過去是接收不到的。
上述內(nèi)容就是postMessage如何在HTML5中使用,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享名稱:postMessage如何在HTML5中使用
標(biāo)題來源:http://jinyejixie.com/article36/pgigpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站維護(hù)、ChatGPT、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站收錄、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)