raam86
raam86

Reputation: 6871

What is the reasoning behind window and top property of window?

I've been reading the great Eloquent JavaScript and came across this:

window.window === window // true

And

window.top === window // true

And of course

window.top === window.top.top.top.window //true 

This is all expected since the object is pointing to itself. And I also understand window holds all of the global JavaScript variables including itself but:

  1. What is the use of this self pointer?
  2. Why do we need top to point to same pointer
  3. Uses for window.top?

Upvotes: 0

Views: 66

Answers (1)

nicosantangelo
nicosantangelo

Reputation: 13726

top

window.top refers to the top-most window from a window nested in one or more layers of <iframe> sub-windows

self (or window)

From the MDCN page for window.self:

if (window.parent.frames[0] != window.self) {
   // this window is not the first frame in the list
}

window.self is almost always used in comparisons like in the example above, which finds out if the current window is the first subframe in the parent frameset.

Upvotes: 2

Related Questions