利用html5 postMessage介面跨網域設定iframe大小

來源:互聯網
上載者:User

標籤:oct   ash   eve   att   except   nts   between   htm   end   

<!doctype html><html>    <head>        <title>Document A</title>        <meta charset="utf-8">    </head>    <body>        <iframe src="http://remote-domain.com:8080/document-B.html" id="zino_iframe"></iframe>        <script type="text/javascript">        var zino_resize = function (event) {            if (event.origin !== "http://remote-domain.com:8080") {                return;            }            var zino_iframe = document.getElementById(‘zino_iframe‘);            if (zino_iframe) {                zino_iframe.style.height = event.data + "px";            }        };        if (window.addEventListener) {            window.addEventListener("message", zino_resize, false);        } else if (window.attachEvent) {            window.attachEvent("onmessage", zino_resize);        }        </script>    </body></html>

 

<!doctype html><html>    <head>        <title>Document B</title>        <meta charset="utf-8">        <script type="text/javascript">        function iframe_resize(){            var body = document.body,            html = document.documentElement,            height = Math.max(body.scrollHeight, body.offsetHeight,         html.clientHeight, html.scrollHeight, html.offsetHeight);            if (parent.postMessage) {                parent.postMessage(height, "http://my-domain.com");            }        }        </script>    </head>    <body onload="iframe_resize();">        <h4>Document B</h4><p>Cross-Domain Iframe</p>    </body></html>

 

Normally, documents on different pages are able to communicate between each other only if their domains, protocols and ports match up. HTML 5 specification comes with window.postMessage, which provides cross-domain communication between scripts.

Syntax

window.postMessage(message, targetOrigin, [transfer]);

message
Messages can be nested objects and arrays, can contain JavaScript values (strings, numbers, Dates, etc), and can contain certain data objects such as File Blob, FileList, and ArrayBuffer objects.
targetOrigin
The value must be either asterisk *, slash / or absolute URL. Otherwise a SyntaxError exception will be throw. If you know the window location, you should always provide this specific location instead of just putting an asterisk *
transfer (Optional)
These objects are transferred with the message, and they are no longer usable on the sending side.

利用html5 postMessage介面跨網域設定iframe大小

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.