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 our opener's opener.
12 function postToOpenerOfOpener(msg
, origin
) {
13 window
.opener
.opener
.postMessage(msg
, origin
);
17 // Send a message to a window named "foo".
18 function postToFoo(msg
) {
19 var w
= window
.open("", "foo");
20 w
.postMessage(msg
, "*");
24 function openPopup(url
) {
25 popup
= window
.open(url
);
28 function postToPopup(msg
) {
29 popup
.postMessage(msg
, "*");
33 // Send a message to parent.
34 function postToParent(msg
) {
35 parent
.postMessage(msg
, "*");
39 // Send a message to sibling.
40 function postToSibling(msg
, sibling
) {
41 parent
.frames
[sibling
].postMessage(msg
, "*");
45 // Send a message to a subframe of window named "foo".
46 function postToFooFrame(msg
) {
47 var w
= window
.open("", "foo");
48 w
.frames
[0][0].postMessage(msg
, "*");
52 function registerUnload() {
53 window
.addEventListener('unload', function(e
) {
54 postToParent("message-from-unload", "*");
58 // Listen to incoming messages.
59 var receivedMessages
= 0;
60 var receivedMessagesWithPort
= 0;
61 window
.addEventListener("message", messageReceived
, false);
62 function messageReceived(event
) {
64 if (event
.data
=== "2-1-reply") {
65 event
.source
.postMessage("msg4", "*");
66 } else if (event
.data
=== "subframe-msg") {
67 event
.source
.postMessage("subframe-msg-reply", "*");
68 } else if (event
.data
=== "subframe-msg-reply") {
69 domAutomationController
.setAutomationId(0);
70 domAutomationController
.send("done-" + window
.name
);
72 if (event
.ports
&& event
.ports
.length
> 0) {
73 receivedMessagesWithPort
++;
74 var port
= event
.ports
[0];
76 port
.postMessage("msg-back-via-port");
77 // Change the title to generate a notification.
78 document
.title
= event
.data
.message
;
80 document
.title
= event
.data
;