Access parent in javascript from iFrame / Window

How to access a global object or array defined in the parent window of a 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'm in a mini document, I would like to have access to the events_data variable in the javascript function.

+4
source share
1 answer

Option 1

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

 window.opener.events_data 

Check window.opener in MDN .

Option 2

Your code indicates that you are using an iframe. From iframe, just use parent :

 parent.events_data; 

Check window.parent in MDN .


window.opener - returns a link to the window that opens this current window.

window.parent - When a window loads in a, or, its parent is a window with an element, a window attachment.

+8
source

Source: https://habr.com/ru/post/1402264/


All Articles