In JavaScript, let’s say we have a main page (main.html) which contains an <iframe> (iframe.html).
Now inside this iframe.html, if we need to refer to something on the main page (main.html), can we not just specify window instead of parent.window.
If the answer is we need to write parent.window, I wanted to understand is there not a single window object reference for all the iframes within a main page…
While I do understand document is specific to individual iframes, but window should be common to all..Isn’t it…Please help me in understanding the concept…
Also is there something window.parent as well? If yes, how does it differ from parent.window?
The concept of
windowis tied to thedocument: There’s onewindowperdocument, and onedocumentperwindow.That means
<iframe>elements, which have their owndocument, also have their ownwindow, just like a pop-up window or the main navigator window.So, you’ll indeed have to use
window.parentto access the container of an<iframe>element, just like you have to usewindow.openerto access the owner of a pop-up window.EDIT: Both
window.parentandparent.windoware valid expressions that return the same object. That’s because thewindowobject is the default context in scripting (unqualified names are parsed as members ofwindow), andwindowobjects have awindowproperty that refers to themselves.So,
parent.windowis evaluated aswindow.parent.window, which is the same object aswindow.parent.That said, I do prefer using
window.parent, to avoid the (minimal) overhead associated with the extra property access.