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 our parent's opener.
18 function postToOpenerOfParent(msg
, origin
) {
19 parent
.opener
.postMessage(msg
, origin
);
23 // Send a message to our parent's opener.
24 function postToOpenerOfSibling(siblingName
, msg
, origin
) {
25 parent
.frames
[siblingName
].opener
.postMessage(msg
, origin
);
29 // Send a message to a window named "foo".
30 function postToFoo(msg
) {
31 var w
= window
.open("", "foo");
32 w
.postMessage(msg
, "*");
36 function openPopup(url
) {
37 popup
= window
.open(url
);
40 function postToPopup(msg
) {
41 popup
.postMessage(msg
, "*");
45 // Send a message to parent.
46 function postToParent(msg
) {
47 parent
.postMessage(msg
, "*");
51 // Send a message to sibling.
52 function postToSibling(msg
, sibling
) {
53 parent
.frames
[sibling
].postMessage(msg
, "*");
57 // Send a message to a subframe of window named "foo".
58 function postToFooFrame(msg
) {
59 var w
= window
.open("", "foo");
60 w
.frames
[0][0].postMessage(msg
, "*");
64 function registerUnload() {
65 window
.addEventListener('unload', function(e
) {
66 postToParent("message-from-unload", "*");
70 // Listen to incoming messages.
71 var receivedMessages
= 0;
72 var receivedMessagesWithPort
= 0;
73 window
.addEventListener("message", messageReceived
, false);
74 function messageReceived(event
) {
76 if (event
.data
=== "2-1-reply") {
77 event
.source
.postMessage("msg4", "*");
78 } else if (event
.data
=== "subframe-msg") {
79 event
.source
.postMessage("subframe-msg-reply", "*");
80 } else if (event
.data
=== "subframe-msg-reply") {
81 domAutomationController
.setAutomationId(0);
82 domAutomationController
.send("done-" + window
.name
);
84 if (event
.ports
&& event
.ports
.length
> 0) {
85 receivedMessagesWithPort
++;
86 var port
= event
.ports
[0];
88 port
.postMessage("msg-back-via-port");
89 // Change the title to generate a notification.
90 document
.title
= event
.data
.message
;
92 document
.title
= event
.data
;