跨域問題2

使用window.postmessage方法來跨域


Html5里有一個屬性window.postmessage也可以用來跨域:

postmessage(data,origin)有兩個參數(shù):

data: 要傳遞的數(shù)據(jù),html5規(guī)范中該參數(shù)可以是JavaScript的任意基本類型或可復(fù)制的對象,考慮到部分瀏覽器只能處理字符串參數(shù),所以在傳遞參數(shù)的時候需要使用JSON.stringify()方法對對象參數(shù)序列化,對postmessage()方法的支持度為IE8+;

origin: 字符串參數(shù),指明目標(biāo)窗口的源;設(shè)置為* 則為通配,這樣可以傳遞給任意窗口,如果要指定和當(dāng)前窗口同源的話設(shè)置為"/"

例如有兩個頁面:

http://test.com/index.html

http://receive.com/index.html

http://test.com/index.html中發(fā)送信息:

<script>

var obj= {

key: 'values'

}

window.onload=function(){

win.postMessage(obj,'http://receive.com/index.html');

}

</script>

然后再在http://receive.com/index.html中接受消息,渲染顯示:


window.onmessage=function(e){

if(e.origin !== 'http://test.com/index.html') return; //做一下安全性判斷,看看消息是否是由可信源頭發(fā)送

console.log(e.origin+' '+e.data.key); //接受跨域數(shù)據(jù),渲染

}

//http://test.com/index.html values

window.postmessage()也可用在iframe的通信中,例如(該實例來源于:http://www.cnblogs.com/dolphinX/p/3464056.html):

<!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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Section1、為什么要跨域? 自古以來(1995年起),為了用戶的信息安全,瀏覽器就引入了同源策略。那么同源策...
    qhaobaba閱讀 468評論 0 0
  • Section1、為什么要跨域? 自古以來(1995年起),為了用戶的信息安全,瀏覽器就引入了同源策略。那么同源策...
    不去解釋閱讀 690評論 0 0
  • 1. 什么是跨域? 跨域一詞從字面意思看,就是跨域名嘛,但實際上跨域的范圍絕對不止那么狹隘。具體概念如下:只要協(xié)議...
    他在發(fā)呆閱讀 861評論 0 0
  • 原文來自:http://www.ruanyifeng.com/blog/2016/04/same-origin-p...
    神秘者007閱讀 1,143評論 0 1
  • 1. 什么是跨域? 跨域一詞從字面意思看,就是跨域名嘛,但實際上跨域的范圍絕對不止那么狹隘。具體概念如下:只要協(xié)議...
    w_zhuan閱讀 622評論 0 0

友情鏈接更多精彩內(nèi)容