javascript Iframe 内容窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4765826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:48:32  来源:igfitidea点击:

Iframe ContentWindow

javascripthtmliframeweb

提问by TimelierSword

When I try to get the contentwindow out of an iframe, using

当我尝试从 iframe 中获取 contentwindow 时,使用

var contentWindow = document.getElementbyId('iframe').contentWindow 

sometimes it returns "Window undefined" because the contentWindow doesn't exist. I can't seem to run a check for it using

有时它会返回“Window undefined”,因为 contentWindow 不存在。我似乎无法使用

if (contentWindow === unidentified) or if (contentWindow === null)

as it just errors out of the code if I try to grab values out of it. Has anyone else run into this problem and figured out a solution?

因为如果我试图从中获取值,它只是代码中的错误。有没有其他人遇到过这个问题并找到了解决方案?

回答by meder omuraliev

Are you typing it properly? getElementByIDnot getElementbyId. Did you confirm that that returns an element before querying contentWindow? Are you querying it after it loads?

你打字正确吗?getElementByID不是getElementbyId。您是否确认在查询之前返回了一个元素contentWindow?你在加载后查询它吗?

Are you doing gEBIafter DOM ready or page load? Did you throw in alerts on the element? Is the domain in the iframe the same as the origin domain? What browser are you using?

你是gEBI在 DOM 准备好还是页面加载后做的?您是否对元素发出警报?iframe 中的域是否与原始域相同?你使用的是什么浏览器?

if (contentWindow === unidentified) or if (contentWindow === null)

There is no such thing as unidentifiedit's undefined. Slow down and be accurate.

目前是没有这样的东西unidentifiedundefined。放慢速度并保持准确。

回答by Wazy

Trythis

试试这个

var iframeElem = parent.document.getElementById("iframe");
var win = iframeElem.contentWindow;

回答by Decor

Try specifying what data you want from the object you call here

尝试从您在此处调用的对象中指定您想要的数据

var contentWindow = document.getElementById('iframe')

by doing this

通过做这个

var contentWindow = document.getElementById('iframe').contentWindow

This worked for me

这对我有用