親ウィンドウで iframe 関数を呼び出すにはどうすればよいですか。以下のようなことをしましたが、Firefox では機能していないようです。同じコードがクロムで完全に機能します。
window.frames["original_preview_iframe"].window.exportAndView(img_id);
親ウィンドウで iframe 関数を呼び出すにはどうすればよいですか。以下のようなことをしましたが、Firefox では機能していないようです。同じコードがクロムで完全に機能します。
window.frames["original_preview_iframe"].window.exportAndView(img_id);
私はあなたが使用しなければならないと思います
document.getElementById('target_Frame').contentWindow.callingtargetFunction();
それ以外の場合は、この URL を使用して問題の解決策を説明してください
これをお勧めします https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
私のために働いた明確なウィキの例:
var o = document.getElementsByTagName('iframe')[0];
o.contentWindow.postMessage('Hello B', 'http://example.com/');
そして、iframeで:
function receiver(event) {
if (event.origin == 'http://example.net') {
if (event.data == 'Hello B') {
event.source.postMessage('Hello A, how are you?', event.origin);
}
else {
alert(event.data);
}
}
}
window.addEventListener('message', receiver, false);
iframe 関数を呼び出す方法はいくつかあります。
iframe id はoriginal_preview_iframe
トリガーに使用できますdocument.getElementById("original_preview_iframe").contentWindow.exportAndView()
。
を使用しwindow.frames
ます。は配列です。「test.html」で
window.frames
iframe名を設定
できます。次に、配列を反復処理し、名前を比較してからトリガーします。window.name="this is iframe test"
for (let i = 0; i < window.frames.length; i++) {
if (window.frames[i].name === "this is iframe test") {
window.frames[i].exportAndView()
}
}
postMessage を使用します。
way1 と way2 では、オブジェクトに関数を割り当てる必要がありwindow
ます。
<body>
<script>
// this one
window.exportAndView = function(){}
// or this one
function exportAndView(){}
</script>
</body>
Way3 では、exportAndView を非表示にしてからトリガーすることもできます。
ここに例があります。
// a.html
<html>
<body>
<iframe id="original_preview_iframe" src="/b.html">
</iframe>
<script>
// let postMessage trigger after b.html load
setTimeout(function(){
document.getElementById("original_preview_iframe").contentWindow.postMessage({data: "hi"});
}, 500)
</script>
</body>
</html>
// b.html (iframe html)
<html>
<body>
<script>
(function() {
function exportAndView() {
console.log("test");
}
window.addEventListener("message", (event) => {
exportAndView()
})
})()
</script>
</body>
</html>
次に、a.html で、のように way1 または way2 を使用してみることができますdocument.getElementById("original_preview_iframe").contentWindow.exportAndView()
。
exportAndView
スコープの問題のため、呼び出されません。