使用tampermonkey,我在页面的 iframe 中运行此 Javascript:
document.addEventListener("message", function(e){
console.log('asd');
}, false);
这样我就可以从父窗口接收消息。我正在使用 jQuery Mobile 并拥有以下 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>
但是当我在浏览器控制台中这样做时:
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