3 <head><title>Post message tests
</title>
5 // Send a message to our opener, and it will reply.
6 function postToOpener(msg
, origin
) {
7 window
.opener
.postMessage(msg
, origin
);
11 // Send a message to a window named "foo".
12 function postToFoo(msg
) {
13 var w
= window
.open("", "foo");
14 w
.postMessage(msg
, "*");
18 // Send a message to a subframe of window named "foo".
19 function postToFooFrame(msg
) {
20 var w
= window
.open("", "foo");
21 w
.frames
[0][0].postMessage(msg
, "*");
25 // Listen to incoming messages.
26 var receivedMessages
= 0;
27 var receivedMessagesWithPort
= 0;
28 window
.addEventListener("message", messageReceived
, false);
29 function messageReceived(event
) {
31 if (event
.data
=== "2-1-reply") {
32 event
.source
.postMessage("msg4", "*");
34 if (event
.ports
.length
> 0) {
35 receivedMessagesWithPort
++;
36 var port
= event
.ports
[0];
38 port
.postMessage("msg-back-via-port");
39 // Change the title to generate a notification.
40 document
.title
= event
.data
.message
;
42 document
.title
= event
.data
;