ロードイベントをトリガーする代わりに、IFrameによって送信されたメッセージを使用する必要があります。
外枠内のJavaScript(これはメッセージのイベントリスナを付加し、メッセージを受信し、アラートを示す):
window.addEventListener('message', function(e) {
// This check can be removed for testing purposes, but for security
// it's strongly recommended that you leave it in there and replace
// the domain with the one of your IFrame's src, to ensure you process
// only messages coming from your own code and not somebody else's.
if(e.origin != "http://yourdomain.com") return;
alert("Got a message: " + e.data);
});
JavaScriptの内枠に(これは親ウィンドウにメッセージを送信する - それが必要)は、リスナーをトリガし、アラートを表示:
// The second parameter can be replaced by "*" for testing, but again it
// is strongly recommended to use the domain you expect the outer frame
// to have, to ensure your message lands in the right window (in case
// another page loaded yours in an IFrame).
window.parent.postMessage("Hello World!", "http://yourdomain.com");
代わりの"Hello World!"
、あなたはまた、JSのオブジェクトを渡すことができます。
他の方法でも動作します:IFrameにメッセージリスナーをインストールする場合は、document.getElementById('myIframe').contentWindow.postMessage(...)
などを使用して、外部フレームから内部フレームにメッセージを送信することもできます。
も参照してください:http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage
質問は申し訳ありませんが、確かに間違っていました。それは2年間そのように働いて、私は私のスクリプトを忘れました。私は起源を保存し、それを再利用するよりも、問題があった:Facebookのメッセージが後に来てそれを変更(私はもはやメッセージを送信できませんでした)。質問を削除していただき、ありがとうございます。 – skobaljic