6

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.

James Hill
  • 60,353
  • 20
  • 145
  • 161
user823527
  • 3,644
  • 17
  • 66
  • 110

1 Answers1

11

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.

James Hill
  • 60,353
  • 20
  • 145
  • 161
  • And what about permission deniel ? If browser doesn't allow to access parent properties from iframe ? – Vitalii Lebediev Mar 20 '12 at 13:30
  • If I'm not mistaken, this happens when the contents of iframe is loaded from another domain – Vitalii Lebediev Mar 20 '12 at 13:40
  • 1
    @Malgin, you're correct. Cross domain scripting is a huge security risk and is therefore not allowed. You can tell that the OP is not attempting to do that because the `src` attribute of his iframe is using a relative path. – James Hill Mar 20 '12 at 13:46
  • @JamesHill All that is true, but if this is required ? Here's my question and the way I'm going to do that in my app ([click](http://stackoverflow.com/questions/9788328/access-parent-window-from-iframe-cross-domain)) – Vitalii Lebediev Mar 20 '12 at 14:06
  • @Malgin, ask these questions in your thread, not in the comments of this question. – James Hill Mar 20 '12 at 14:10