Set variable in parent window from iframe

后端 未结 2 1966
情深已故
情深已故 2020-12-01 07:22

I have a parent document with an embedded iframe. Inside the iframe I have an upload field. Once the user selects a file to upload, I trigger a jQuery change event. On th

2条回答
  •  爱一瞬间的悲伤
    2020-12-01 08:00

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

    This means that

    var foo = 'bar';
    

    is analogous to

    window.foo = 'bar';
    

    Which means that you can read the global scope of any window object you can obtain a reference to. What we can also imply here is that usage of window is implicit. Even when you don't explicitly type "window.", it's there anyway.

    And since frames themselves are also auto-exposed as DOM properties of the current window object, this means you can access any other frames' window object as well.

    The parent property of window objects holds a reference the window object of that window's parent (if there is one). Since iframes most certainly have a parent window, then all this stuff I just typed boils down to this

    // set the global variable 'foo' in the parent global scope
    parent.foo = 'bar';
    

提交回复
热议问题