Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access iframe window object within parent DOM

I have project in which I have an elements from other domains. I'm using JavaScript to access first iframe window object into variable. Here is code :

var iframes = window.frames;

//grab first iframe
var ifrWindow = iframes[0].window;  // Here is where I get **Permision denied**

ifrWindow.postMessage("hello",IframeDomain);

I'm getting 'Permission denied' only for IE8. I have no problems with Chrome, Firefox, Safari or later versions IE11, etc..

Anyone has experienced this kind of issue with IE8?

like image 792
SlowCoder Avatar asked Dec 08 '14 05:12

SlowCoder


People also ask

Can an iframe access its parent DOM?

If the content of the iframe and its parent have the same domain, you can access the parent pages DOM from the iframe by using parent. document.

How do I get the window object of an iframe?

The contentWindow property returns the Window object of an HTMLIFrameElement. You can use this Window object to access the iframe's document and its internal DOM. This attribute is read-only, but its properties can be manipulated like the global Window object.

Can iframe access parent windows?

The window will get opened by using the iframe, and the communication could be done with the parent window from the child window. To call a parent window function, use the following syntax and also refer to the code given below.


1 Answers

Have you tried contentWindow or contentDocument method?

Something like this should work:

var iframe = document.getElementById("myframe");
var iframeWindow = (iframe.contentWindow || iframe.contentDocument);
like image 138
Thilak Rao Avatar answered Sep 23 '22 00:09

Thilak Rao