How to initiate a JavaScript function in an iframe to run in the parent page?

痴心易碎 提交于 2019-12-11 10:54:52

问题


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();

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?


回答1:


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

Like

var parentRef = this;

so the child could run

parentRef.myFunction();


来源:https://stackoverflow.com/questions/18275601/how-to-initiate-a-javascript-function-in-an-iframe-to-run-in-the-parent-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!