Cross-window messaging with postMessage
Ref: http://javascript.info/tutorial/cross-window-messaging-with-postmessage#example
Cross-window messaging API is supported by all modern browsers including IE8. It allows windows/frames from multiple domains to communicate with each other.
To send a message to another window referenced by
Syntax is
By pressing the button, you send the message to iframe on another domain. The browser controls that the domain must be
Note that the browser guarantees that
Usually you’d want to filter who may send messages/commands to iframe and who may not.
In IE8, it is possible to use
Works in all madern major browsers including Chrome/Safari, Firefox, Opera and IE8.
IE8 doesn’t allow to
Ref:
http://javascript.info/tutorial/cross-window-messaging-with-postmessage#example
http://javascript.info/tutorial/frames-and-windows
Cross-window messaging API is supported by all modern browsers including IE8. It allows windows/frames from multiple domains to communicate with each other.
To send a message to another window referenced by
win
, the postMessage method is used.Syntax is
postMessage(data, targetDomain)
, where:- data
- The message. Accordin to the specification, it could be any object. But as of now, only strings are supported in major browsers.
- targetDomain
- Limit receiving iframe by given domain. Can contain ‘*’ which doesn’t put any restrictions.
targetDomain
argument for better security.Example
Let’s see how it works from the sending side.01 | < iframe src = "http://a.JavaScript.info/files/tutorial/window/receive.html" id = "iframe" style = "height:60px" ></ iframe > |
02 |
03 | < form name = "form" > |
04 | < input type = "text" name = "msg" value = "Your message" /> |
05 | < input type = "submit" /> |
06 | </ form > |
07 |
08 | <script> |
09 |
10 | var win = document.getElementById( "iframe" ).contentWindow |
11 |
12 | document.forms.form.onsubmit = function () { |
13 | win.postMessage( |
14 | this .elements.msg.value, |
15 | "http://a.JavaScript.info" |
16 | ) |
17 | return false |
18 | } |
19 |
20 | </script> |
a.JavaScript.info
(targetDomain).The receiving side
The receiving side hooks on amessage
event. The source of the iframe
, used in the example above:01 | < div id = "test" >Send me a message!</ div > |
02 | <script> |
03 | function listener(event){ |
04 | if ( event.origin !== "http://javascript.info" ) |
05 | return |
06 |
07 | document.getElementById( "test" ).innerHTML = "received: " +event.data |
08 | } |
09 |
10 | if (window.addEventListener){ |
11 | addEventListener( "message" , listener, false ) |
12 | } else { |
13 | attachEvent( "onmessage" , listener) |
14 | } |
15 | </script> |
event.origin
contains the real domain which sent the message. There is no way for JavaScript hacker to replace it.Usually you’d want to filter who may send messages/commands to iframe and who may not.
Cross-window messaging security model is two-sided. The sender ensures that the receiving domain is
The special propertys of a message event are:targetDomain
. The receiver checks that the message came from proper event.origin
.- data
- The first argument of
postMessage
- origin
- The source domain
- source
- The reference to sending window. It is possible to respond by calling
event.source.postMessage('..response message..', event.origin)
.
Nifty details
The messages are passed using internal browser API. So there is no network communication at all.In IE8, it is possible to use
win.postMessage
for iframes only, not for other tabs/windows. That’s the bug of implementation. Other browsers don’t suffer from that.Summary
ThepostMessage
API allows to communicate between windows in a safe, cross-browser way.Works in all madern major browsers including Chrome/Safari, Firefox, Opera and IE8.
IE8 doesn’t allow to
postMessage
to other windows, only to iframes.Ref:
http://javascript.info/tutorial/cross-window-messaging-with-postmessage#example
http://javascript.info/tutorial/frames-and-windows
评论
发表评论