Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access iframe's javascript objects from parent page?

Is it possible to access iframe's javascript objects from javascript of parent page?

like image 340
Dims Avatar asked Nov 28 '11 12:11

Dims


People also ask

How to get iframe object in JavaScript?

Getting the element in Iframeconst iframe = document. getElementById("myIframe"); Now, it has and contentWindow property which returns the document object by using that we can access the elements from an Iframe. const iWindow = iframe.

How to access parent window from iframe in JavaScript?

To access elements of parent window from iframe with JavaScript, we can use the window. parent. document property to get the parent window's document.

How do I access iframe parent?

Once id of iframe is set, you can access iframe from inner document as shown below. var iframe = parent. document. getElementById(frameElement.id);

Can an iframe get parent URL?

Yes, accessing parent page's URL is not allowed if the iframe and the main page are not in the same (sub)domain. However, if you just need the URL of the main page (i.e. the browser URL), you can try this: var url = (window. location !=


2 Answers

Try this. I assume your iFrame's id is "targetFrame" and the object you want to call is targetObject and also they are same domain. If they are not same domain I think it is not possible.:

document.getElementById('targetFrame').contentWindow.targetObject
like image 74
erimerturk Avatar answered Oct 07 '22 19:10

erimerturk


If the iframe src is not in the same origin/domain as the parent you'd have restrictions

because of the same-origin-policy (security policy) https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript

The policy is planned to block accessing private data such as CSRF tokens and other Private data stored in the frame, for example if you build an html page with an iframe of bank.com then when people get into your site if they are logged into bank.com their private data will be stored in the iframe for that reason same-origin-policy will block different origins from accessing eachother's data through iframes.

like image 39
Ben Avatar answered Oct 07 '22 17:10

Ben