从父窗口调用iframe函数

时间:2012-07-07 06:30:38

标签: javascript jquery

如何从父窗口调用ifram中的javascript函数。 我有一个包含iframe的jsp页面。 jsp页面由“a.yenama.net”服务器提供,iframe服务于“b.yenama.net”服务器。

<iframe width="1200" height="640" name="data" id="dataId" >
</iframe>   
<br/>
<input type="button" name="data" value="Save data" onclick="callSaveData();" />

尝试从父jsp页面下面的代码并在IE中收到权限被拒绝错误

window.frames['data'].callData();

也尝试了

document.getElementById('dataId').contentWindow.callData(); //didn't work

iframe中的功能

window.callData = function(){
   alert('Iframe function');
}

真的很感激你的帮助。

2 个答案:

答案 0 :(得分:1)

你可以通过在一个普通的.js文件中声明你的函数来做到这一点 因此,您可以从任何地方访问您的功能。

度过愉快的一天

答案 1 :(得分:1)

我建议您使用jquery.load方法而不是iframe在第一页的div中加载第二页,然后您可以从两个页面调用任何方法

$('#result').load('PageWhichYouAreOpeningInIFrame.html');

或者如果您仍想使用IFrame,则可以使用:

Invoking JavaScript code in an iframe from the parent page

http://api.jquery.com/load/