iframe_messageChannel_pingpong.html 788 B

12345678910111213141516171819202122232425262728293031323334
  1. <!DOCTYPE HTML>
  2. <html>
  3. <body>
  4. <script type="application/javascript">
  5. function ok(what, msg) {
  6. window.parent.postMessage({type: what ? 'OK' : 'KO', msg: msg }, '*');
  7. }
  8. window.addEventListener('message', receiveMessage, false);
  9. function receiveMessage(evt) {
  10. if (evt.data.type == 'PORT') {
  11. var port = evt.data.port;
  12. var counter = 0;
  13. port.onmessage = function(evt) {
  14. if (counter++ == 0) {
  15. ok(!(evt.data % 2), "The number " + evt.data + " has been received correctly by the iframe");
  16. window.parent.postMessage({ type: 'PORT', port: port }, '*', [port]);
  17. }
  18. else {
  19. ok(false, "Wrong message!");
  20. }
  21. }
  22. } else {
  23. ok(false, "Unknown message");
  24. }
  25. }
  26. </script>
  27. </body>
  28. </html>