postMessage_onOther.html (3012B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>postMessage called through another frame</title> 5 <script type="application/javascript"> 6 var PATH = "/tests/dom/tests/mochitest/whatwg/postMessage_onOther.html"; 7 8 function receiveMessage(evt) 9 { 10 if (evt.lastEventId !== "") 11 { 12 fail("unexpected non-empty lastEventId"); 13 return; 14 } 15 16 switch (window.location.href) 17 { 18 case "http://example.com" + PATH: 19 receiveTopDomain(evt); 20 break; 21 22 case "http://test1.example.com" + PATH: 23 receiveSubDomain(evt); 24 break; 25 26 default: 27 fail("unexpected location"); 28 } 29 } 30 31 function fail(msg) 32 { 33 window.parent.postMessage("FAIL " + msg, "*"); 34 } 35 36 // The parent frame sends "start-test" to the subdomain frame to start. 37 // The subdomain frame then sets document.domain to the top domain so that 38 // the top domain frame can access it. It then sends a message to the top 39 // domain frame to tell it to do likewise; once that happens, the top domain 40 // frame can then call a method on the subdomain frame window, which will 41 // call a method *on the top domain window* to send a message to the parent 42 // window. We thus expect to see an event whose source is the subdomain 43 // window -- *not* the top domain window. Therefore, its .origin should be: 44 // 45 // http://test1.example.com 46 // 47 // and not 48 // 49 // http://example.com 50 51 function receiveSubDomain(evt) 52 { 53 if (evt.origin !== "http://mochi.test:8888") 54 { 55 fail("wrong top-domain origin: " + evt.origin); 56 return; 57 } 58 if (evt.data !== "start-test") 59 { 60 fail("wrong top-domain message: " + evt.origin); 61 return; 62 } 63 64 document.domain = "example.com"; 65 window.parent.topDomainFrame.postMessage("domain-switch", 66 "http://example.com"); 67 } 68 69 function receiveTopDomain(evt) 70 { 71 if (evt.origin !== "http://test1.example.com") 72 { 73 fail("wrong subdomain origin: " + evt.origin); 74 return; 75 } 76 if (evt.data !== "domain-switch") 77 { 78 fail("wrong subdomain message: " + evt.origin); 79 return; 80 } 81 if (evt.source !== window.parent.subDomainFrame) 82 { 83 fail("wrong source on message from subdomain"); 84 return; 85 } 86 87 document.domain = "example.com"; 88 window.parent.subDomainFrame.testSiblingPostMessage(); 89 } 90 91 function testSiblingPostMessage() 92 { 93 window.parent.postMessage("test-finished", "http://mochi.test:8888"); 94 } 95 96 function setup() 97 { 98 var target = document.getElementById("location"); 99 target.textContent = location.hostname + ":" + (location.port || 80); 100 } 101 102 window.addEventListener("message", receiveMessage); 103 window.addEventListener("load", setup); 104 </script> 105 </head> 106 <body> 107 <h1 id="location">No location!</h1> 108 </body> 109 </html>