50

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?

Milan Babuškov
  • 57,554
  • 49
  • 122
  • 178
  • 9
    When using Web Workers self and window are not the same thing. See [http://stackoverflow.com/questions/11219775/global-variable-in-web-worker][1] [1]: http://stackoverflow.com/questions/11219775/global-variable-in-web-worker – user1872904 Jun 02 '14 at 13:45

4 Answers4

26

self is a read-only property that can be more flexible than, and sometimes used in favor of, the window directly. This is because self's reference changes depending on the operating context (unlike window.self, which only exists if window exists). It's also great for comparisons, as others have mentioned.

For example, if you use self inside a Web Worker (which lives in its own background thread), self will actually reference WorkerGlobalScope.self. However, if you use self in a normal browser context, self will simply return a reference to Window.self (the one that has document, addEventListener(), and all the other stuff you're used to seeing).

TL;DR while the .self in window.self will not exist if window doesn't exist, using self on its own will point to Window.self in a traditional window/browser context, or WorkerGlobalScope.self in a web worker context.

As usual, MDN has a great writeup on this subject in their JavaScript docs. :)


Side note: The usage of self here should not be confused with the common JS pattern of declaring a local variable: var self = this to maintain a reference to a context after switching.

You can read more about that here: Getting Out of Binding Situations in JavaScript.

Titus
  • 4,407
  • 6
  • 30
  • 40
22

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
  • 14,775
  • 8
  • 35
  • 51
  • 1
    Except necro-bug if FF6.0, http://stackoverflow.com/a/7769187/139361, `window` and `self` should be identical. Anyone knows any other bugs? --------------------------------------- I must add that using `self` is confusing to most javascript developers, so use `window` instead. – Dan Jan 16 '14 at 10:17
13

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

Qantas 94 Heavy
  • 15,410
  • 31
  • 63
  • 82
Jesse Dhillon
  • 7,561
  • 32
  • 34
  • 1
    For code that needs to run independent of a browser, it seems better to use `self`, i.e. headless execution. – nilskp Jun 04 '13 at 14:22
  • @nilskp, that's interesting, but I did not understand you. Could you please explain? – Dan Jan 13 '14 at 09:37
  • @Dan, `window` is only present in a GUI (browser). Not all Javascript code runs in a browser. In those cases, `self` will still work, but `window` will not. – nilskp Jan 13 '14 at 21:15
  • @nilskp, I think the topic is about difference between `window` and `window.self`. If there is no `window`, `self` would automatically disappear. I found nothing about magic `self` variable in node.js. On the other side, PhanomJS emulates true window object. Do you know any other non-browser environments where javascript executes, where `self` is provided? – Dan Jan 16 '14 at 10:26
  • 1
    @Dan, Rhino (Java implementation). – nilskp Jan 21 '14 at 22:26
  • 5
    Given that `window === window.self` is `true` (in FF and Chrome, at least), why not just use `window.parent.frames[0] !== window`? – Carl G Apr 16 '14 at 15:18
5

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

davehauser
  • 5,704
  • 4
  • 29
  • 45