简体   繁体   中英

Access parent object in JavaScript from iFrame/Window

How do I access a global object or array defined in a parent window in the child window.

<script>
    var events_data;
    function function_to_fill_events_data () {
      .
      .
      .
    }
</script>

<div>
    <div><iframe src="mini.php" width:100%; height: 100%;" scrolling="no"></iframe> </div>
</div>

When I am in the mini document I'd like to be able to access the events_data variable in a javascript function.

Option 1

Your title mentions a child window. If you have a child window, and not an iframe, use this:

window.opener.events_data

Check out window.opener on MDN .

Option 2

Your code indicates that you're using an iframe. From an iframe, simply use parent :

parent.events_data;

Check out window.parent on MDN .


window.opener - Returns a reference to the window that opened this current window.

window.parent - When a window is loaded in an , , or , its parent is the window with the element embedding the window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM