Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iframe calling parent javascript

Is it possible for me to setup an iframe so it calls a javascript function available in the window.parent.document scope?

like image 651
Webnet Avatar asked Mar 29 '11 18:03

Webnet


People also ask

How do you call a parent page of an iframe function?

To call a parent window function, use “window. top”.

Can iframe access parents?

When a page is running inside of an iframe, the parent object is different than the window object. You can still access parent from within an iframe even though you can't access anything useful on it. This code will never cause an error even when crossing origins.

Can an iframe run Javascript?

Calling a parent JS function from iframe is possible, but only when both the parent and the page loaded in the iframe are from same domain i.e. example.com , and both are using same protocol i.e. both are either on http:// or https:// .

Will iframe work if Javascript is disabled?

If a user has javascript disabled, iframes will work. An iframe tag has attributes “height” and “width,” which allows the designer great latitude with dimensions and format like 300×250 , 728×90 depending on the Ad size. Iframe tag can appear anywhere on the page and several iframes can be added if wished to.


1 Answers

If I am understanding the question correctly, assuming functionName() is defined within the parent of iframe, you can do this within your iframe:

window.parent.functionName(); 

This will only work though, if both parent and iframe are on the same domain: http://softwareas.com/cross-domain-communication-with-iframes

like image 63
e11s Avatar answered Sep 21 '22 00:09

e11s