View Full Version : hopefully simple question about javascript and frames

08-25-2010, 03:07 PM
I'm building a webpage using javascript and iframes. Basically I have an iframe in the middle of the index.html page that links to another html page (let's call it iframe.html). My question is, is it possible to call a javascript function from iframe.html to control an object on index.html? If so, how do I do this? I'd like to be able to assign an image in iframe.html with the hyperlink of href="javascript:function()", where the function effects the CSS of a div on index.html.


Old Pedant
08-25-2010, 07:20 PM
*IF* the two HTML files come from the same domain, the answer is "yes".

From JS in the <iframe>, you can refer to the main page using "parent.".

So something like

parent.document.getElementById("image37").src = "foo.jpg";

will work fine.

You might find it better to supply functions in the main page to do the manipulations and then just invoke the functions from the iframe's code. Again, just by parent.callSomeFunction(x,y,13); or whatever is needed.

Old Pedant
08-25-2010, 07:21 PM
Should have said: If the two pages come from different domains, then *NO* cross-domain scripting is possible. Period.