postMessagePortal.html 页face代码
标题<script></p>
<p>var targetOrigin = "http://22527.vhost20.boxcdn.cn";</p>
<p>var defaultTitle = "포털";<br>varnotificationTimer = null;</p>
<p>function messageHandler(e) {<br> if (e.origin == targetOrigin) {<br> inform(e.data);<br> } else {<br> // 다른 출처의 메시지 무시<br> }<br>}</p>
<p>function sendString(s) {<br> document.getElementById("widget").contentWindow.postMessage(s, targetOrigin);<br>}</p>
<p><br>function inform(message) {<br> stopBlinking();<br> 깜박임(message, defaultTitle);<br>}</p>
<p>function stopBlinking() {<br> if (notificationTimer !== null) {<br>clearTimeout(notificationTimer);<br> }<br> document.title = defaultTitle;<br>}</p> ;
<p>function flickTitle(m1, m2) {<br> document.title = m1;<br>notificationTimer = setTimeout(blinkTitle, 1000, m2, m1)<br>}</p>
<p>function sendStatus() {<br> var statusText = document.getElementById("statusText").value;<br> sendString(statusText);<br>}</p>
<p>function loadDemo() {<br> document.getElementById("sendButton").addEventListener("click", sendStatus, true);<br> document.getElementById("stopButton").addEventListener("click", stopBlinking, true);<br> sendStatus();<br>}<br>window.addEventListener("load", loadDemo, true);<br>window.addEventListener("message", messageHandler, true);< /p>
<p></script>
跨域communica
传递信息:
postMessageWidget.html页面的代码
标题<script>< /p>
<p>var targetOrigin = "http://www.weixiu0376.cn";</p>
<p>// TODO 화이트리스트 배열</p>
<p>function messageHandler(e) {<br> if (e.origin === "http://www.weixiu0376.cn") {<br> document.getElementById("status").textContent = e. data;<br> } else {<br> // 다른 출처에서 온 메시지 무시<br> }<br>}</p>
<p>function sendString(s) {<br> window.top.postMessage(s, targetOrigin);<br>}</p>
<p>function loadDemo() {<br> document.getElementById("actionButton").addEventListener("click",<br> function() {<br> var messageText = document.getElementById("messageText").value ;<br> sendString(messageText);<br> }, true);</p>
<p>}<br>window.addEventListener("load", loadDemo, true);<br>window.addEventListener("message", messageHandler, true);</p>
<p></script>
显示接收信息:
< ;input type="text" id="messageText" value="填写消息内容">