在父级顶部显示脚本

Show script in parent top

本文关键字:显示 脚本 顶部      更新时间:2023-09-26

我有这个脚本(是一个弹出窗口):

var script = document.createElement('script');
script.setAttribute('async', 'async');
script.src = 'https://scriptexample.js';
document.body.appendChild(script);

当我放这段代码时,它通常工作得很好,但当代码在iframe中时,它就不工作了,只在iframe中显示。

我需要对代码中的哪些内容进行更改,以便调用"top/pparent"位置并显示脚本(即使它在iframe中)?

感谢

主文档和iFrame的文档是不同的文档,具有不同的变量范围。存档的唯一方法是使用
window.parent

从iFrame调用父窗口中加载的脚本,并使用

iframeElement.contentWindow

以访问加载到iFrame的脚本。

读取:

http://www.w3schools.com/jsref/prop_frame_contentwindow.asp

https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentWindow

https://developer.mozilla.org/en-US/docs/Web/API/Window/parent

示例:

在主窗口中定义了test函数,您可以从iFrame:中以这种方式调用它

window.parent.test(...);