from: stackoverflow, original:http://stackoverflow.com/questions/14366841/accessing-javascript-variable-in-an-iframe-from-the-parent-window-on-same-domai
You are already executing the script when the <iframe>
is still loading, so it’s test
is undefined
. Either wait for the iframe’s load
event, or just get the variable when clicking:
1 2 3 4 5 6 | <iframe id="iframe" height="100" src="iframe.html" width="200"></iframe> <script> var myFrame = window.frames[0].window; </script> <a onclick="alert(myFrame.test)" id="link" href="#">Click</a> |