溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

HTML5中postMessage實現(xiàn)跨域的方法

發(fā)布時間:2020-10-23 15:01:41 來源:億速云 閱讀:111 作者:小新 欄目:web開發(fā)


對于使用H5實現(xiàn)跨域,很多人都一直處于半懂狀態(tài)。知道使用postMessage發(fā)送消息,使用onMessage接受消息,但是到底哪個方法應該用window調(diào)用哪個應該用iframe的contentWindow調(diào)用不是很清楚。下面是我做的一個本地實現(xiàn)跨域的小demo,可以在github下載這個示例。為了執(zhí)行它,首先,你需要找到你電腦的hosts文件,在127.0.0.1 localhost下添加如下代碼:

127.0.0.1   localhost
127.0.0.1   main.com
127.0.0.1   A.com
127.0.0.1   B.com

然后,你需要啟動一個服務器,如Apache等,把github上下載的三個html文件放到你的服務器上。最后,你只需訪問http://main.com:你的端口號 ,就可以進行跨域通信了。

三個html文件的關系如下:三個域:http://main.com:8090 ; http://a.com:8090 ; http://b.com:8090 。 主頁面maindomain.html在main.com,兩個iframe (subAdomain.html , subBdomain.html)分別在 a.com , b.com 。在maindomain.html中,向textarea中輸入消息,點擊send to iframe按鈕,可以發(fā)送消息到指定iframe (subAdomain.html 或者subBdomain.html),在ifame中也可以發(fā)送消息到maindomain.html,同時,有一個收到ifame消息的回執(zhí)信息。

這應該是很常見的場景,把網(wǎng)站公共的資源放到某子域,在其他子域需要訪問該子域上的資源。

基本知識

首先介紹onMessage事件中,event的一些屬性,理解這些可以使你很容易讀懂我的示例。
* data: 傳入的數(shù)據(jù)
* origin: 發(fā)送消息的文檔所在的域
* source: 發(fā)送消息的文檔的window對象的代理
如果你想在子域X向子域Y發(fā)送消息,你需要,在子域X的html文件,獲取Y的window對象(iframe的contentWindow),然后調(diào)用postMessage(message, Y所在的域),同時,在子域Y的html文件中,監(jiān)聽window對象message事件(使用onMessage)就好。當然,你可以在onMessage中再次使用postMessage,向子域X發(fā)送一個回執(zhí)信息。 我們時?;靵y的是,在哪個域的window對象上調(diào)用postMessage。

代碼

main.com

    <h2>This is the main domain</h2>
    <div style="margin:0 20px;">
        <textarea name="main" cols="80" rows="5"></textarea><br/>
        <input type="button" value="send to iframe A"/>
        <input type="button" value="send to iframe B"/>
    </div>
    <div style="float:left; margin:0 20px;">
        <h4>iframe A</h4>
        <iframe src="http://a.com:8090/subAdomain.html" frameborder="1" style="width:300px; height:300px;"></iframe>
    </div>
    <div style="float:left;">
        <h4>iframe B</h4>
        <iframe src="http://b.com:8090/subBdomain.html" frameborder="1" style="width:300px; height:300px;"></iframe>
    </div>
    <div style="float:left;">
        <h6 id="received"></h6>
    </div>
    <script>
        var received = document.querySelector('#received');
        var sendToIframeA = document.querySelectorAll('input')[0];
        var sendToIframeB = document.querySelectorAll('input')[1];
        var iframeA = document.querySelectorAll('iframe')[0];
        var iframeB = document.querySelectorAll('iframe')[1];
 
        //receive message
        function getMessage(e){
            console.log('main received!');
            received.innerHTML = 'Receive message from ' + e.origin + ', the data is ' + e.data;
            e.source.postMessage('Received the message', e.origin);
        }
        window.addEventListener('message', getMessage, false);
 
        //post message
        sendToIframeA.addEventListener('click', function(){
            var content = document.querySelector('textarea').value;
            iframeA.contentWindow.postMessage(content, 'http://a.com:8090');
        }, false);
        sendToIframeB.addEventListener('click', function(){
            var content = document.querySelector('textarea').value;
            iframeB.contentWindow.postMessage(content, 'http://b.com:8090');
        }, false);
    </script>
  • a.com

       <h6>This is domain A</h6>
    <textarea name="subA" cols="30" rows="10"></textarea>
    <input type="button" value="send to parent"/>
    <div style="float:left;">
        <h6 id="received"></h6>
    </div>
    <script>
        var send = document.querySelector('input');
        var text = document.querySelector('textarea');
        var received = document.querySelector('#received');
 
        //receive message
        function getMessage(e){
            console.log('A received!');
            received.innerHTML = 'Receive message from ' + e.origin + ', the data is ' + e.data;
            //e.source.postMessage('Received the message', e.origin);
        }
        window.addEventListener('message', getMessage, false);
 
        //post message
        send.addEventListener('click', function(){
            var content = text.value;
            window.parent.postMessage(content, 'http://main.com:8090');
        }, false);
    </script>

b.com

    <h6>This is domain B</h6>
    <textarea name="subB" cols="30" rows="10"></textarea>
    <input type="button" value="send to parent"/>
    <div style="float:left;">
        <h6 id="received"></h6>
    </div>
    <script>
        var send = document.querySelector('input');
        var text = document.querySelector('textarea');
        var received = document.querySelector('#received');
 
        //receive message
        function getMessage(e){
            console.log('B received!');
            received.innerHTML = 'Receive message from ' + e.origin + ', the data is ' + e.data;
            //e.source.postMessage('Received the message', e.origin);
        }
        window.addEventListener('message', getMessage, false);
 
        //post message
        send.addEventListener('click', function(){
            var content = text.value;
            window.parent.postMessage(content, 'http://main.com:8090');
        }, false);
    </script>

關于HTML5中postMessage實現(xiàn)跨域的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI