HTML5中怎么樣使用postMessageAPI

這篇文章主要介紹HTML5中怎么樣使用postMessage API,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元三河做網(wǎng)站,已為上家服務(wù),為三河各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):18982081108

關(guān)于postMessage

window.postMessage雖然說(shuō)是html5的功能,但是支持IE8+,假如你的網(wǎng)站不需要支持IE6和IE7,那么可以使用window.postMessage。關(guān)于window.postMessage,很多朋友說(shuō)他可以支持跨域,不錯(cuò),window.postMessage是客戶(hù)端和客戶(hù)端直接的數(shù)據(jù)傳遞,既可以跨域傳遞,也可以同域傳遞。

應(yīng)用場(chǎng)景

我只是簡(jiǎn)單的舉一個(gè)應(yīng)用場(chǎng)景,當(dāng)然,這個(gè)功能很多地方可以使用。

假如你有一個(gè)頁(yè)面,頁(yè)面中拿到部分用戶(hù)信息,點(diǎn)擊進(jìn)入另外一個(gè)頁(yè)面,另外的頁(yè)面默認(rèn)是取不到用戶(hù)信息的,你可以通過(guò)window.postMessage把部分用戶(hù)信息傳到這個(gè)頁(yè)面中。(當(dāng)然,你要考慮安全性等方面。)

代碼舉例

發(fā)送信息:

//彈出一個(gè)新窗口   
var domain = 'http://haorooms.com';   
var myPopup = window.open(domain    
            + '/windowPostMessageListener.html','myWindow');   
  
//周期性的發(fā)送消息   
setTimeout(function(){   
    //var message = '當(dāng)前時(shí)間是 ' + (new Date().getTime());    
        var message = {name:"站點(diǎn)",sex:"男"}; //你在這里也可以傳遞一些數(shù)據(jù),obj等   
    console.log('傳遞的數(shù)據(jù)是  ' + message);   
    myPopup.postMessage(message,domain);   
},1000);

要延遲一下,我們一般用計(jì)時(shí)器setTimeout延遲再發(fā)用。

接受的頁(yè)面

//監(jiān)聽(tīng)消息反饋   
window.addEventListener('message',function(event) {   
    if(event.origin !== 'http://haorooms.com') return; //這個(gè)判斷一下是不是我這個(gè)域名跳轉(zhuǎn)過(guò)來(lái)的   
    console.log('received response:  ',event.data);   
},false);

如下圖,接受頁(yè)面得到數(shù)據(jù)

HTML5中怎么樣使用postMessage API

如果是使用iframe,代碼應(yīng)該這樣寫(xiě):

//捕獲iframe   
var domain = 'http://haorooms.com';   
var iframe = document.getElementById('myIFrame').contentWindow;   
  
//發(fā)送消息   
setTimeout(function(){   
    //var message = '當(dāng)前時(shí)間是 ' + (new Date().getTime());    
        var message = {name:"站點(diǎn)",sex:"男"}; //你在這里也可以傳遞一些數(shù)據(jù),obj等   
    console.log('傳遞的數(shù)據(jù)是:  ' + message);   
        //send the message and target URI   
    iframe.postMessage(message,domain);    
},1000);

接受數(shù)據(jù)

//響應(yīng)事件   
window.addEventListener('message',function(event) {   
    if(event.origin !== 'http://haorooms.com') return;   
    console.log('message received:  ' + event.data,event);   
    event.source.postMessage('holla back youngin!',event.origin);   
},false);

上面的代碼片段是往消息源反饋信息,確認(rèn)消息已經(jīng)收到。下面是幾個(gè)比較重要的事件屬性:

source – 消息源,消息的發(fā)送窗口/iframe。
origin – 消息源的URI(可能包含協(xié)議、域名和端口),用來(lái)驗(yàn)證數(shù)據(jù)源。
data – 發(fā)送方發(fā)送給接收方的數(shù)據(jù)。

調(diào)用實(shí)例
1. 主線(xiàn)程中創(chuàng)建 Worker 實(shí)例,并監(jiān)聽(tīng) onmessage 事件

<html>    
<head>    
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">    
<title>Test Web worker</title>    
<script type="text/JavaScript">    
 function init(){    
  var worker = new Worker('compute.js');    
  //event 參數(shù)中有 data 屬性,就是子線(xiàn)程中返回的結(jié)果數(shù)據(jù)   
  worker.onmessage= function (event) {    
   // 把子線(xiàn)程返回的結(jié)果添加到 p 上   
   document.getElementById("result").innerHTML +=    
      event.data+"<br/>";    
  };    
 }    
</script>    
</head>    
<body onload="init()">    
<p id="result"></p>    
</body>    
</html>

在客戶(hù)端的 compute.js 中,只是簡(jiǎn)單的重復(fù)多次加和操作,最后通過(guò) postMessage 方法把結(jié)果返回給主線(xiàn)程,目的就是等待一段時(shí)間。而在這段時(shí)間內(nèi),主線(xiàn)程不應(yīng)該被阻塞,用戶(hù)可以通過(guò)拖拽瀏覽器,變大縮小瀏覽器窗口等操作測(cè)試這一現(xiàn)象。這個(gè)非阻塞主線(xiàn)程的結(jié)果就是 Web Workers 想達(dá)到的目的。

2. compute.js 中調(diào)用 postMessage 方法返回計(jì)算結(jié)果

var i=0;    
  
function timedCount(){    
 for(var j=0,sum=0;j<100;j++){    
  for(var i=0;i<100000000;i++){    
   sum+=i;    
  }    
 }    
 // 調(diào)用 postMessage 向主線(xiàn)程發(fā)送消息   
 postMessage(sum);    
}    
  
postMessage("Before computing,"+new Date());    
timedCount();    
postMessage("After computing,"+new Date());

以上是“HTML5中怎么樣使用postMessage API”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

名稱(chēng)欄目:HTML5中怎么樣使用postMessageAPI
網(wǎng)頁(yè)網(wǎng)址:http://muchs.cn/article46/ipishg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化虛擬主機(jī)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、定制網(wǎng)站、動(dòng)態(tài)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)