You're apparently not waiting for the frame's content to be loaded before accessing myVar. Chances are the <script> element in the frame has not yet been fetched or executed when you do that.
Try delaying the variable assignment until the frame's content is fully loaded:
<iframe src="iframe.html" id="iframe" onload="frameLoaded();" width="200" height="100"></iframe> <script> var myVar; function frameLoaded() { var myVar = window.frames[0].window.test; // The rest of your code that depends on 'myVar'... } </script> As an aside, since your question is tagged jquery I would suggest you write something like:
<iframe src="iframe.html" id="iframe" width="200" height="100"></iframe> <script> $("#iframe").on("load", function() { var myVar = this.window.test; //$("#link").on("click", Thefunction() rest{ of your code that depends on 'myVar'... alert(myVar); }); }); </script>