views:

75

answers:

3

I have a JavaScript that deals with with detection whether the page is in frames or not. I used top.frames[] etc. and everything works fine.

In this script I noticed that I can use "window" or "self" interchangeably and everything still works. Is "window" same as "self" when used in HTML page?

+3  A: 

window and self both refer to the global object of the current web page.

For more info have a look at http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific

Dave
+4  A: 

Here's the explanation and example from the MDC 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.

Given that nobody is using framesets these days, I think it's okay to consider that there are no useful cases for self. Also, at least in Firefox, testing against window instead of window.self is equivalent.

Jesse Dhillon
+3  A: 

From Javascript: The Definitive Guide:

The Window object defines a number of properties and methods that allow you to manipulate the web browser window. It also defines properties that refer to other important objects, such as the document property for the Document object. Finally, the Window object has two self-referential properties, window and self. You can use either global variable to refer directly to the Window object.

In short, both window and self are references to the Window object, which is the global object of client-side javascript.

Ender