Is it possible to access iframe's javascript objects from javascript of parent page?
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.
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.
Once id of iframe is set, you can access iframe from inner document as shown below. var iframe = parent. document. getElementById(frameElement.id);
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 !=
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With