Set variable in parent window from iframe

I have a parent document with an inline iframe. Inside the iframe, I have a field to load. When the user selects the file to upload, I fire the jQuery change event. In this case, I want to set the variable in the parent window to true so that the parent knows that the download has started.

Does anyone know how to do this?

Tried this but didn't work:

var test; $("#newsletter_email").change(function() { parent.window.test = true; }); $('#send').click(function() { if (test) { alert('File has been uploaded!'); } else { alert('You need to upload a file'); } }); 
+41
javascript events iframe
Aug 19 '09 at 17:48
source share
2 answers

Variables in the global scope are automatically exposed as the DOM properties of their containing window object.

It means that

 var foo = 'bar'; 

similarly

 window.foo = 'bar'; 

This means that you can read the global area of ​​any window object that you can reference. Here we can also imply that the use of window implicit. Even when you do not explicitly enter a "window", it is all the same.

And since the frames themselves are also automatically displayed as DOM properties of the current window object, this means that you can also access any other window object.

The parent property of window objects contains a reference to the window object of this window parent (if any). Since the iframe certainly has a parent window, then all of these things that I just printed boil down to this

 // set the global variable 'foo' in the parent global scope parent.foo = 'bar'; 
+82
Aug 19 '09 at 18:05
source share

You can also save it to localStorage and read and write from it, provided that both the main document and iframe use the same domain.

+3
Apr 23 '13 at 11:52
source share



All Articles