访问Iframe中的Javascript变量 [英] Accessing Javascript Variables Within An Iframe

查看:95
本文介绍了访问Iframe中的Javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要访问我的iframe源代码,我注意到它的值为srt,我可以通过在iframes Source中输入srt来访问此变量。但是,当它是iframe时,键入srt不再有效,因为变量嵌套在iframe中。

If i was to visit the source of my iframe, I notice that it has a value "srt" , I can access this variable by simply typing "srt" When in the iframes Source. However when it is an "iframe" typing "srt" no longer works since the variable is nested within the iframe.

如何存储变量的值,即在iframe中,对于不在iframe中的变量,要在外部访问此值。

How can i store the value of a variable that is in an iframe, To a variable that is not In the iframe, to access this value Outside.

推荐答案

您可以使用<按索引或框架名称收集href =https://developer.mozilla.org/en-US/docs/Web/API/Window.frames =nofollow>框架。集合中的每个元素都是该框架的窗口对象,这是存储全局变量的地方。

You can use the frames collection by index or by the frame's name. Each element in the collection is the window object for that frame, which is where global variables are stored.

frames[0].srt // if there's just one
frames['frameName'].srt  // accessing by the name property

或者您可以获取iframe元素并使用 contentWindow 检索iframe的窗口对象

Or you can get the iframe element and use contentWindow to retrieve the iframe's window object

 document.getElementById('frameId').contentWindow.srt;

还要记住,您无法访问不在同一域中的帧。

Also remember that you can't access a frame that is not in the same domain.

这篇关于访问Iframe中的Javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆