从iframe将javascript函数注入父窗口 [英] Inject javascript function into parent window from iframe

查看:103
本文介绍了从iframe将javascript函数注入父窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,该项目包含许多在父窗口中打开和关闭的iframe。我有一个需要在iframe之间定义和共享的对象。我遇到的问题是,当顶部创建对象的iframe关闭时,与之关联的函数将丢失。

I'm working on a project that has many iframes that are opened and closed in a parent window. I have an object that needs to be defined and shared between the iframes. The issue I'm running into is that when the iframe that created the object on top is closed the functions associated with it are lost.

我有没有办法定义即使在原始iframe超出范围之后,也可以从我的iframe访问顶部的对象?

Is there a way I can define an object on top that will be accessible from my iframes even after the original iframe goes out of scope?

推荐答案

如果是其他人偶然发现了这一点。我发现Javascript函数存储为引用,因此从iframe定义top.func = function(){...}将在顶部创建对函数的引用。这意味着如果iframe丢失,那么函数也是如此。

In case someone else stumbles across this. What I've found is that Javascript functions are stored as references, so defining top.func = function(){...} from an iframe will create a reference to the function on the top. This means that if the iframe is lost so is the function.

我的解决方案是创建一个隐藏在后台的iframe,并在需要定义该函数时创建。

My solution was to create an iframe that's hidden in the background and created when that function needs to be defined.

这篇关于从iframe将javascript函数注入父窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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