2

How to call a javascript function in ifram from parent window. i have a jsp page which contains an iframe. jsp page is served by "a.yenama.net" server and iframe is served "b.yenama.net" server.

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

tried below code from parent jsp page and recieved permission denied error in IE

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

also tried

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

Function in iframe

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

your help is truly appreciated.

amj
  • 383
  • 1
  • 5
  • 13

2 Answers2

1

you can do that by declaring your function in a common .js file Therefore, you can access your function from wherever you want.

Have a nice day

sdespont
  • 13,915
  • 9
  • 56
  • 97
  • common function isn't possible, as the iframe is content is served by a different team(like a third party) – amj Jul 07 '12 at 16:07
1

i would suggest you to use jquery.load method instead of iframe to load your second page in a div of first page and then you can call any methods from both the pages

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

or if you still wants to go with IFrame then you can use:

Invoking JavaScript code in an iframe from the parent page

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

Community
  • 1
  • 1
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178