Skip to main content
added 48 characters in body
Source Link
Frédéric Hamidi
  • 263.8k
  • 42
  • 497
  • 486

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 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> 

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> 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 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; // The rest of your code that depends on 'myVar'... }); </script> 

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() { myVar = window.frames[0].window.test; } </script> 

As an aside, since your question is tagged 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", function() {   alert(myVar); }); }); </script> 
added 383 characters in body
Source Link
Frédéric Hamidi
  • 263.8k
  • 42
  • 497
  • 486

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> 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 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; // The rest of your code that depends on 'myVar'... }); </script> 

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> function frameLoaded() { var myVar = window.frames[0].window.test; // The rest of your code that depends on 'myVar'... } </script> 

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> 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 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; // The rest of your code that depends on 'myVar'... }); </script> 
Source Link
Frédéric Hamidi
  • 263.8k
  • 42
  • 497
  • 486

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> function frameLoaded() { var myVar = window.frames[0].window.test; // The rest of your code that depends on 'myVar'... } </script>