0

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 ??

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.