在父页面的iframe中调用JavaScript代码 [英] Invoking JavaScript code in an iframe from the parent page

查看:114
本文介绍了在父页面的iframe中调用JavaScript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我在页面中嵌入了一个 iframe ,而 iframe 有一些 JavaScript 例程我需要从父页面调用。



现在相反很简单,因为你只需要调用 parent.functionName(),但不幸的是,我需要完全相反的。



请注意,我的问题并未改变<$的来源 URL c $ c> iframe ,但调用在 iframe中定义的函数

解决方案

假设你的iFrame的id是targetFrame,你想调用的函数是 targetFunction()

  document.getElementById('targetFrame')。contentWindow.targetFunction(); 

您也可以使用 window.frames 代替 document.getElementById

  //此选项的作用不适用于大多数最新版本的Chrome和Firefox 
window.frames [0] .frameElement.contentWindow.targetFunction();


Basically, I have an iframe embedded in a page and the iframe has some JavaScript routines I need to invoke from the parent page.

Now the opposite is quite simple as you only need to call parent.functionName(), but unfortunately, I need exactly the opposite of that.

Please note that my problem is not changing the source URL of the iframe, but invoking a function defined in the iframe.

解决方案

Assume your iFrame's id is "targetFrame" and the function you want to call is targetFunction():

document.getElementById('targetFrame').contentWindow.targetFunction();

You can also access the frame using window.frames instead of document.getElementById.

// this option does not work in most of latest versions of chrome and Firefox
window.frames[0].frameElement.contentWindow.targetFunction(); 

这篇关于在父页面的iframe中调用JavaScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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