Access a variable of iframe from parent

css, html, javascript, jquery

Solution

Using `contentWindow` instead of `contentDocument` works for me:

var check = document.getElementById("iframeid").contentWindow.a;

Also, ensure that the domains match and that you are using a webserver to test (I got a protocol warning when testing from the file system).

UPDATE: You're almost definitely better to use the `postMessage` API.

Problem

script of iframe ``` <script type="text/javascript" > var a=5; </script> ``` script of parent window ``` <script type="text/javascript" > function close() { var check=document.getElementById("iframeid").contentDocument.a; alert(check) } </script> ``` I want to access the variable which is defined inside the iframe from parent. But the above code doesn't work properly can anyone give an idea to implement this.

Original source

Related problems