如何在iframe中启动JavaScript函数以在父页面中运行? [英] How to initiate a JavaScript function in an iframe to run in the parent page?

查看:83
本文介绍了如何在iframe中启动JavaScript函数以在父页面中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在父文件中有一个函数,该函数取决于几个全局变量.我让孩子iframe用

I have a function in a parent file that depends on several global variables. I am having the child iframe update one of the parent's global variables with

parent.myvar = "myvalue";

效果很好(请注意:所有内容都在同一个域中).现在,我需要在父级中运行该函数以完成该过程.问题:

Works great (note: everything is on the same domain). Now I need to run the function in the parent to complete the process. Problem:

parent.myfunction();

我刚刚运行了我需要减去父文件中任何全局变量的函数(它在子iframe中执行).当然,我可以通过重新声明所有全局变量来从父文件中移走所有全局变量(我什至可以让父函数进行所有重新封装).但是,我希望有一个更优雅的解决方案.有什么方法可以让孩子启动要在父环境中运行的功能?

I have just run the function I need minus any of the global variables in the parent file (it is being executed within the child iframe). Of course, I could bring over all the global variables from the parent file by just redeclaring all my global variables (I could even have the parent function do all the redeclairing). However, I was hoping for a more elegant solution. Is there any way to get a child to initiate a function to be run within the parent environment?

推荐答案

我不确定我是否理解这个问题,但是您不能将父级的变量引用传递给孩子吗?

I'm not sure if I understand the question, but couldn't you pass in a variable reference of parent to the child?

喜欢

var parentRef = this;

这样孩子就可以奔跑

parentRef.myFunction();

这篇关于如何在iframe中启动JavaScript函数以在父页面中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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