主頁(yè) > 知識(shí)庫(kù) > 詳解html5 postMessage解決跨域通信的問(wèn)題

詳解html5 postMessage解決跨域通信的問(wèn)題

熱門標(biāo)簽:南寧點(diǎn)撥外呼系統(tǒng)哪家公司做的好 江蘇智能電銷機(jī)器人哪家好 鎮(zhèn)江智能外呼系統(tǒng)有效果嗎 黃島區(qū)地圖標(biāo)注 四川點(diǎn)撥外呼系統(tǒng) 云南大理400電話申請(qǐng)官方 成都智能外呼系統(tǒng)平臺(tái) 當(dāng)涂高德地圖標(biāo)注 電銷機(jī)器人電話用什么卡

本文介紹了詳解html5 postMessage解決跨域通信的問(wèn)題,分享給大家,具體如下:

效果圖

postmessage解析HTML5提供了新型機(jī)制PostMessage實(shí)現(xiàn)安全的跨源通信. 語(yǔ)法 otherWindow.postMessage(message, targetOrigin, [transfer]);

otherWindow: 其他窗口的一個(gè)引用, 比如IFRAME的contentWindow屬性, 執(zhí)行,window.open返回的窗口對(duì)象. message: 將要發(fā)送到其他窗口的數(shù)據(jù). targetOrigin:通過(guò)窗口的origin屬性來(lái)指定哪些窗口能接收到消息事件, 其值可以是字符”*”(表示無(wú)限制)或者一個(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,就是捕獲傳遞。實(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ù)訪問(wèn)</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>

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

標(biāo)簽:廣西 酒泉 十堰 咸寧 西寧 佳木斯 淮安 南京

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解html5 postMessage解決跨域通信的問(wèn)題》,本文關(guān)鍵詞  詳解,html5,postMessage,解決,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《詳解html5 postMessage解決跨域通信的問(wèn)題》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于詳解html5 postMessage解決跨域通信的問(wèn)題的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章