本文介紹了詳解html5 postMessage解決跨域通信的問題,分享給大家,具體如下:
效果圖:

postmessage解析HTML5提供了新型機制PostMessage實現安全的跨源通信.
語法:
otherWindow.postMessage(message, targetOrigin, [transfer]);
otherWindow:其他窗口的一個引用, 比如IFRAME的contentWindow屬性, 執行,window.open返回的窗口對象。
message:將要發送到其他窗口的數據。
targetOrigin:通過窗口的origin屬性來指定哪些窗口能接收到消息事件, 其值可以是字符”*”(表示無限制)或者一個URL transfer:是一串和message同時傳遞的Transferable對象。 這些對象的所有權將被轉移給消息的接收方, 而發送一放將不再保有所有權。
element.addEventListener(event,fn,useCaption ); 三個參數 event 事件 比如click mouseenter mouseleave 回調函數 useCaption用于描述是冒泡還是捕獲。默認值是false,即冒泡傳遞。 當值為true,就是捕獲傳遞。實現方式
主界面 main.html
!DOCTYPE html html lang= en head meta charset= UTF-8 meta name= viewport content= width=device-width, initial-scale=1.0 meta http-equiv= X-UA-Compatible content= ie=edge title 跨域數據訪問 /title script type= text/javascript window.addEventListener( message ,function(e){ console.log( e--- ,e); const data = e.data; document.getElementById( main1 ).style.backgroundColor=e.data; },false) /script /head body p id= main1 >iframe界面
!DOCTYPE html html lang= en head meta charset= UTF-8 meta name= viewport content= width=device-width, initial-scale=1.0 meta http-equiv= X-UA-Compatible content= ie=edge title Document /title style type= text/css html,body{ height:100%; margin:0px; /style /head body >總結:以上就是本篇文的全部內容,希望能對大家的學習有所幫助。更多相關教程請訪問Html5視頻教程!
相關推薦:
php公益培訓視頻教程
HTML5圖文教程
HTML5在線手冊
以上就是詳解html5 postMessage解決跨域通信的問題的詳細內容,其它編程語言
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答