這篇文章將為大家詳細(xì)講解有關(guān)html5新機(jī)制:postMessage如何實(shí)現(xiàn)安全跨域通信,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),美蘭網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:美蘭等地區(qū)。美蘭做網(wǎng)站價(jià)格咨詢:18982081108效果圖postmessage解析HTML5提供了新型機(jī)制PostMessage實(shí)現(xiàn)安全的跨源通信. 語法
otherWindow.postMessage(message, targetOrigin, [transfer]);
otherWindow: 其他窗口的一個(gè)引用, 比如IFRAME的contentWindow屬性, 執(zhí)行,
window.open返回的窗口對(duì)象. message: 將要發(fā)送到其他窗口的數(shù)據(jù). targetOrigin:
通過窗口的origin屬性來指定哪些窗口能接收到消息事件, 其值可以是字符”*”(表示無限制)或者一個(gè)URL transfer:
是一串和message同時(shí)傳遞的Transferable對(duì)象. 這些對(duì)象的所有權(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,就是捕獲傳遞。
主界面 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> <p id="main1" style="width:200px;height:200px;margin:100px;border:solid 1px #000;"> 我是主界面,等待接收iframe的傳遞 </p> <p style="margin:100px;"> iframe <iframe src="http://localhost:3000/iframe.html" width="800px" height="300px" ></iframe> </p> </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%;"> <p id="frame" style="height:200px; width:200px;background-color:rgb(204, 204, 0)" onclick="changeColor()"> 點(diǎn)擊改變顏色 </p> <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>
關(guān)于html5新機(jī)制:postMessage如何實(shí)現(xiàn)安全跨域通信就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁名稱:html5新機(jī)制:postMessage如何實(shí)現(xiàn)安全跨域通信-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article30/dhgjpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、ChatGPT、小程序開發(fā)、網(wǎng)站制作、網(wǎng)站內(nèi)鏈、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容