HTML5-postMessage,postmessage

來源:互聯網
上載者:User

HTML5-postMessage,postmessage

1.window.postMessage訪問能夠安全的實現跨域訪問;

2.不同頁面的scripts如果要實現互相訪問的話,必須要滿足以下幾個條件:

  (1)相同的協議;

  (2)相同的介面;

  (3)相同的主機名稱;

(window.name、location.hash等等其實也是可以實現跨域訪問的)

3.window.postMessage能夠安全的繞過這些限制。

 

1.當window.postMessage方法被調用時,會產生一個MessageEvent,這個event會被派遣到目標視窗;

2.MessageEvent被賦予兩個屬性,一個是要傳輸到目標視窗的資料,另一個是調用postMessage方法的當前視窗所在的域。

 

<!--a.com/a.html--><script>window.onload = function(){var data = 'a';var targetOrigin = 'http://b.com';window.postMessage(data, targetOrigin);}</script>

  

<!--b.com/b.html--><script>window.addEventListener('message', receiveMessage, false);function receiveMessage(event){var origin = event.origin || event.originalEvent.origin;if(origin !== 'http://a.com'){return;}}</script>

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.