English 中文(简体)
从iframe访问父页中的变量
原标题:accessing variable in parent page from iframe

我有一个带有iframe的页面,其中包含一个html页面。我想从iframe中访问父页面中的Javascript变量。主页中变量的名称是observer

我试过这个

parent。observer =  aadasds ;

但是我得到了以下错误:

Permission denied for to get property Window。observer from

最佳回答

只有当两个站点来自同一域时,才允许在iframe(和父站点)之间交换值。如果他们这样做了,你的例子应该会奏效。如果不这样做,浏览器就会禁止通信。

然而,有许多技巧可以绕过这一点:例如Julien le Comte的博客使用第三个iframe来实现单向通信,或者“围绕iframe调整iframe的大小”-Adam Fortuna的博客实现双向交流。

Edit (as people still seem to read this old answer):
In modern Browsers you can use postMessage to exchange Data between iframes. There are many javascript libraries that try to emulate that functionality in older browsers, too. E.g. by mis-using the location.hash, like the jquery-postmessage-plugin does.

问题回答

听起来你的iframe好像在使用不同的域。如果不使用同一域,所有主要浏览器都会阻止对父iframe的访问。IE如果您拥有www.test.com域,并且您嵌入了www.google.com

这个问题的另一个答案解释了实现的API发布消息。这也可以用于发送/接收来自不同域的不同帧的数据。然而,与使用同一域的两个帧相比,您可以使用它做的事情是有限的。

话虽如此,如果你的iframe使用的是同一个域,下面是答案。

window.parent.observer;

希望这能帮助到某人:)





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.