In the JavaScript for the <iframe> I need to call a function in the parent window. Both windows are from the same domain.
Is it safe to use window.parent.myFunction();?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short answer
Yes, you can use
window.parentfrom the same domain.The WhatWG specification chapter 6: Loading Web pages says:
iframe.parentexistsiframe.parentis a proxy to the DOM of theiframe‘s parentReally long, long answer
I am trying to show you how you can come to this conclusion by reading the specification, while keeping in the mind: What are the characteristic traits of accessing
parentfrom within aniframe?Let’s start. It’s a long text.
1. DOM attribute
parentWhatWG#dom-parent says about
window.parent:parentis an attribute to access something like the DOM of the parent window. However:WindowProxydo?Let’s continue reading.
2. Browsing context
A browsing context is an environment in which
Documentobjects are presented to the user.WhatWG#Browsing context says:
3. Window proxy
The window proxy is an object to proxy a
windowobject and to enforce, for example, security constraints. For awindowobject there can be several proxies (for example twoiframesfrom two different domains accessing the same parent).WhatWG#proxy-object says:
4. Security
We are almost there. Keep reading.
WhatWG#security-nav says:
5. Deduction
Can an
iframeaccess functions from itsparent, if both are from the same domain? Lets deduce.parentis not thewindowobject of the parent, but aWindowProxy.iframecan access its parent, because they both have the same origin (see the first security condition above).You see: If you read a specification carefully, you can find answers how browsers should behave. Reading specifications is an acquired skill. It needs patience and exactness.
However: The world is not so nice. There are browsers which do not implement specifications as well as they should. Caveat emptor.