Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the value of an attribute inside an iframe from parent in Javascript?

How can I pass attribute value from iframe to parent?

I.e. iframe (frame1) has <a id="a1" href="http://www.infinitybusiness.net">. How can I get value of href in my parent page?

like image 525
nandu3685 Avatar asked Aug 01 '10 11:08

nandu3685


People also ask

Can an iframe access its parent?

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 we pass parameters to iframe?

You can use a script to get the desired parameter value from parameters passed to page. Show activity on this post. If you have slightly more control on your iframe sandbox, you can try postMessage API to communicate with message on events you desire to trigger.


1 Answers

var fra = document.getElementById('frameId');
// following will work on same domain (or subdomain with document.domain set) only
var fraContent = fra.contentDocument || fra.contentWindow.document;
myLinkHref = fraContent.getElementById('myLinkId').href;

If this is cross-domain, then you will need to have access to add script to the child frame. If not, it can't be done without a server-side proxy.

like image 128
Jhong Avatar answered Sep 19 '22 03:09

Jhong