0

With tampermonkey I am running this Javascript inside an iframe in my page:

document.addEventListener("message", function(e){
  console.log('asd');
}, false);

This way I could receive messages from parent window. I am using jQuery Mobile and have this HTML:

<div data-role="page" data-url="/scraps/new?for=http://en.wikipedia.org/wiki/List_of_hotels_in_Australia" tabindex="0" class="ui-page ui-body-c ui-page-active" style="min-height: 258px;">
    <p class="notice"></p>

    <p class="alert"></p>
    <iframe id="test" name="scrap" src="http://en.wikipedia.org/wiki/List_of_hotels_in_Australia" width="100%"></iframe>
</div>

But when I do this in browser console:

var win = document.getElementById("test").contentWindow; // undefined

win
Unsafe JavaScript attempt to access frame with URL http://en.wikipedia.org/wiki/List_of_hotels_in_Australia from frame with URL http://localhost:3001/scraps/new?for=http://en.wikipedia.org/wiki/List_of_hotels_in_Australia. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL http://en.wikipedia.org/wiki/List_of_hotels_in_Australia from frame with URL http://localhost:3001/scraps/new?for=http://en.wikipedia.org/wiki/List_of_hotels_in_Australia. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL http://en.wikipedia.org/wiki/List_of_hotels_in_Australia from frame with URL http://localhost:3001/scraps/new?for=http://en.wikipedia.org/wiki/List_of_hotels_in_Australia. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL http://en.wikipedia.org/wiki/List_of_hotels_in_Australia from frame with URL http://localhost:3001/scraps/new?for=http://en.wikipedia.org/wiki/List_of_hotels_in_Australia. Domains, protocols and ports must match.
Window {}

win.postMessage('aaaa')
Error: SyntaxError: DOM Exception 12
sites
  • 21,417
  • 17
  • 87
  • 146
  • 1
    Have you heard of the Same Origin Policy? – Mooseman Mar 24 '13 at 22:45
  • Can you tell me why it works here http://ejohn.org/blog/cross-window-messaging/? `One acting as the sender (on ejohn.org), one acting as the receiver (on dev.jquery.com).` Should I need inner iframe to set a Header. – sites Mar 24 '13 at 22:47
  • The demo on the page you linked to is not operational. See http://en.wikipedia.org/wiki/Same_origin_policy – Mooseman Mar 24 '13 at 22:50
  • 1
    @Mooseman the demo above on John Resig's site appears to work fine for me. postMessage() is a method that was created explicitly for the use-case of bypassing Same Origin Policy. – lucideer Apr 29 '13 at 18:04
  • 1
    @juanpastas While postMessage() works cross-domain, it doesn't appear to work between local and remote contexts in my own testing, so you'll either need to run it between two local servers or two remote ones. – lucideer Apr 29 '13 at 18:04
  • That's weird, what does it mean local and remote? local: 127.0.0.1 will communicate with which other? remote: ejohn.org and dev.jquery.com? – sites Apr 29 '13 at 19:13

0 Answers0