I have a page which will sometimes be loaded in an iframe and sometimes just loaded by itself, depending on certain parameters which are not relevant for the purpose of this question. In both cases I want to get the height of the viewport so am using $(parent.window).height(). This seems to return the correct figure in both cases (in and out of an iframe), but I just wanted to check if there is any potential pitfall using $(parent.window).height() in a page which is not in an iframe - i.e. the page technically has no parent.
Otherwise I can structure an if..else with $(parent.window).height() and $(window).height() for in and out of an iframe respectively, but is is necessary? Can I just use $(parent.window).height() to cover both cases?
The docs say that
If a window does not have a parent, its
parentproperty is a reference to itself.
So you're good, window.parent will be a reference to the parent, if one exist, if not, it's a reference to the current window.
There's no need for a condition to check if the window actually has a parent to use the window.parent property, as long as it doesn't matter that it could be the current window that is returned.
You can use window.top to refer to the topmost window. Whether a page is opened normally, or in a frame, or in a nested frame, it will always refer to the top window.