views:

44

answers:

2

Hi,

i have a default.aspx page which contains a iframe, whose source is set to a default.htm page. The default.htm in turn has a link to a script file(). Is it possible to access the functions in the script file in my default.aspx page ?if so how ??

+1  A: 

Yes, if the page in the iframe was loaded from the same domain as the page containing the iframe (this is the same-origin policy).

Note that the iframe and containing page both have a global 'window' object, but they are different.

To access variables and functions from scripts in the inner frame from the outer page, you'd need to be able to reference the iframe as an object. For example, if it's the only iframe on the page, you could just prepend window.frames[0]. to any variable name and you'll be referring to the one declared by the scripts in the iframe. If it's not, then you may have to locate the iframe using an ID:

var iframewindow = document.getElementById('myframe').contentWindow;
// read the content of the 'myvar' variable which was set within that frame.
alert(iframewindow.myvar);

To access the outer page's variables and functions from the inner page, you'd prepend window.parent. to the start of them.

thomasrutter
A: 

Assuming that default.htm is on the same domain as default.aspx, you can use the following javascript to call functions in default.htm:

var myIframe = document.getElementById("iframe_id").contentWindow;
myIframe.func1();
myIframe.func2();
myIframe.func3();
letronje