平時做web開發(fā)的時候關(guān)于消息傳遞,除了客戶端與服務(wù)器傳值還有幾個經(jīng)常會遇到的問題
1.頁面和其打開的新窗口的數(shù)據(jù)傳遞
2.多窗口之間消息傳遞
3.頁面與嵌套的iframe消息傳遞
4.上面三個問題的跨域數(shù)據(jù)傳遞
postMessage()
這些問題都有一些解決辦法,但html5引入的message的API可以更方便、有效、安全的解決這些難題。postMessage()方法允許來自不同源的腳本采用異步方式進(jìn)行有限的通信,可以實(shí)現(xiàn)跨文本檔、多窗口、跨域消息傳遞。
postMessage(data,origin)方法接受兩個參數(shù)
1.data:要傳遞的數(shù)據(jù),html5規(guī)范中提到該參數(shù)可以是JavaScript的任意基本類型或可復(fù)制的對象,然而并不是所有瀏覽器都做到了這點(diǎn)兒,部分瀏覽器只能處理字符串參數(shù),所以我們在傳遞參數(shù)的時候需要使用JSON.stringify()方法對對象參數(shù)序列化,在低版本IE中引用json2.js可以實(shí)現(xiàn)類似效果。
2.origin:字符串參數(shù),指明目標(biāo)窗口的源,協(xié)議+主機(jī)+端口號[+URL],URL會被忽略,所以可以不寫,這個參數(shù)是為了安全考慮,postMessage()方法只會將message傳遞給指定窗口,當(dāng)然如果愿意也可以建參數(shù)設(shè)置為"*",這樣可以傳遞給任意窗口,如果要指定和當(dāng)前窗口同源的話設(shè)置為"/"。
http://test.com/index.html
<div style="width:200px; float:left; margin-right:200px;border:solid 1px #333;"> <div id="color">Frame Color</div> </div> <div> <iframe id="child" src="http://lsLib.com/lsLib.html"></iframe> </div>
我們可以在http://test.com/index.html通過postMessage()方法向跨域的iframe頁面http://lsLib.com/lsLib.html傳遞消息
window.onload=function(){ window.frames[0].postMessage('getcolor','http://lslib.com'); }
接收消息
test.com上面的頁面向lslib.com發(fā)送了消息,那么在lslib.com頁面上如何接收消息呢,監(jiān)聽window的message事件就可以
http://lslib.com/lslib.html
window.addEventListener('message',function(e){ if(e.source!=window.parent) return; var color=container.style.backgroundColor; window.parent.postMessage(color,'*'); },false);
這樣我們就可以接收任何窗口傳遞來的消息了,為了安全起見,我們利用這時候的MessageEvent對象判斷了一下消息源,MessageEvent是一個這樣的東東
有幾個重要屬性
1.data:顧名思義,是傳遞來的message
2.source:發(fā)送消息的窗口對象
3.origin:發(fā)送消息窗口的源(協(xié)議+主機(jī)+端口號)
這樣就可以接收跨域的消息了,我們還可以發(fā)送消息回去,方法類似
簡單的demo
在這個例子中,左邊的div會根據(jù)右邊iframe內(nèi)div顏色變化而變化
<!DOCTYPE html><html><head> <title>Post Message</title></head><body> <div style="width:200px; float:left; margin-right:200px;border:solid 1px #333;"> <div id="color">Frame Color</div> </div> <div> <iframe id="child" src="http://lsLib.com/lsLib.html"></iframe> </div> <script type="text/javascript"> window.onload=function(){ window.frames[0].postMessage('getcolor','http://lslib.com'); } window.addEventListener('message',function(e){ var color=e.data; document.getElementById('color').style.backgroundColor=color; },false); </script></body></html>http://test.com/index.html
<!doctype html><html> <head> <style type="text/css"> html,body{ height:100%; margin:0px; } </style> </head> <body style="height:100%;"> <div id="container" onclick="changeColor();" style="widht:100%; height:100%; background-color:rgb(204, 102, 0);"> click to change color </div> <script type="text/javascript"> var container=document.getElementById('container'); window.addEventListener('message',function(e){ if(e.source!=window.parent) return; var color=container.style.backgroundColor; window.parent.postMessage(color,'*'); },false); function changeColor () { var color=container.style.backgroundColor; if(color=='rgb(204, 102, 0)'){ color='rgb(204, 204, 0)'; }else{ color='rgb(204,102,0)'; } container.style.backgroundColor=color; window.parent.postMessage(color,'*'); } </script> </body></html>http://lslib.com/lslib.html
在例子中頁面加載的時候主頁面向iframe發(fā)送’getColor‘ 請求(參數(shù)沒實(shí)際用處)
window.onload=function(){ window.frames[0].postMessage('getcolor','http://lslib.com'); }
iframe接收消息,并把當(dāng)前顏色發(fā)送給主頁面呢
window.addEventListener('message',function(e){ if(e.source!=window.parent) return; var color=container.style.backgroundColor; window.parent.postMessage(color,'*'); },false);
主頁面接收消息,更改自己div顏色
window.addEventListener('message',function(e){ var color=e.data; document.getElementById('color').style.backgroundColor=color; },false);
當(dāng)點(diǎn)擊iframe事觸發(fā)其變色方法,把最新顏色發(fā)送給主頁面
function changeColor () { var color=container.style.backgroundColor; if(color=='rgb(204, 102, 0)'){ color='rgb(204, 204, 0)'; }else{ color='rgb(204,102,0)'; } container.style.backgroundColor=color; window.parent.postMessage(color,'*'); }
主頁面還是利用剛才監(jiān)聽message事件的程序處理自身變色
window.addEventListener('message',function(e){ var color=e.data; document.getElementById('color').style.backgroundColor=color; },false);
最后
很簡單的用法卻解決了大問題,據(jù)說Facebook已經(jīng)在使用了,而且這也是html5另一個API——web workers傳遞消息的方法,那么它的瀏覽器兼容性怎么樣呢?所謂瀏覽器兼容性幾乎變成了IE幾開始支持的問題了。。。不過好消息是跟localStorage一樣,IE8+都支持了,只不過有些瀏覽器的低版本(比如FireFox4.0)并不支持window.onmessage=function(){}這種寫法,所以我么最好使用事件綁定的寫法,為了兼容IE,也要判斷是否支持addEventListener。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
|
新聞熱點(diǎn)
疑難解答