Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 982379 of Window.self

  • Revision slug: Web/API/Window/self
  • Revision title: Window.self
  • Revision id: 982379
  • Created:
  • Creator: Delapouite
  • Is current revision? Yes
  • Comment added js brush to examples

Revision Content

{{ APIRef() }}

The Window.self read-only property returns the window itself, as a {{domxref("WindowProxy")}}. It can be used with dot notation on a window object (that is, window.self) or standalone (self). The advantage of the standalone notation is that a similar notation exists for non-window contexts, such as in {{domxref("Worker", "Web Workers")}}. By using self, you can refer to the global scope in a way that will work not only in a window context (self will resolve to window.self) but also in a worker context (self will then resolve to {{domxref("WorkerGlobalScope.self")}}).

Syntax

var w = window.self;  // w === window

Example

Uses of window.self like the following could just as well be replaced by window.

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

Furthermore, when executing in the active document of a browsing context, window is a reference to the current global object and thus all of the following are equivalent:

var w1 = window;
var w2 = self;
var w3 = window.window;
var w4 = window.self;
// w1, w2, w3, w4 all strictly equal, but only w2 will function in workers

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', '#dom-self', 'Window.self')}} {{Spec2('HTML WHATWG')}} No difference from the latest snapshot {{SpecName("HTML5.1")}}
{{SpecName('HTML5.1', 'browsers.html#dom-self', 'Window.self')}} {{Spec2('HTML5.1')}} No difference from the {{SpecName("HTML5 W3C")}}
{{SpecName('HTML5 W3C', 'browsers.html#dom-self', 'Window.self')}} {{Spec2('HTML5 W3C')}} First snapshot containing the definition of Window.self.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

  • Its Worker equivalent, {{domxref("WorkerGlobalScope.self")}}.

Revision Source

<p>{{ APIRef() }}</p>

<p>The <code><strong>Window.self</strong></code> read-only property returns the window itself, as a {{domxref("WindowProxy")}}. It can be used with dot notation on a <code>window</code> object (that is, <code>window.self</code>) or standalone (<code>self</code>). The advantage of the standalone notation is that a similar notation exists for non-window contexts, such as in {{domxref("Worker", "Web Workers")}}. By using <code>self</code>, you can refer to the global scope in a way that will work not only in a window context (<code>self</code> will resolve to <code>window.self</code>) but also in a worker context (<code>self</code> will then resolve to {{domxref("WorkerGlobalScope.self")}}).</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="eval">
var w = window.self;  // w === window
</pre>

<h2 id="Notes" name="Notes">Example</h2>

<p>Uses of <code>window.self</code> like the following could just as well be replaced by <code>window</code>.</p>

<pre class="brush:js">
if (window.parent.frames[0]&nbsp;!= window.self) {
    // this window is not the first frame in the list
}
</pre>

<p>Furthermore, when executing in the active document of a browsing context, <code>window</code> is a reference to the current global object and thus all of the following are equivalent:</p>

<pre class="brush:js">
var w1 = window;
var w2 = self;
var w3 = window.window;
var w4 = window.self;
// w1, w2, w3, w4 all strictly equal, but only w2 will function in workers
</pre>

<h2 id="Specification" name="Specification">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', '#dom-self', 'Window.self')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>No difference from the latest snapshot {{SpecName("HTML5.1")}}</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5.1', 'browsers.html#dom-self', 'Window.self')}}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td>No difference from the {{SpecName("HTML5 W3C")}}</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 W3C', 'browsers.html#dom-self', 'Window.self')}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td>First snapshot containing the definition of <code>Window.self</code>.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 class="noinclude" id="See_also">See also</h2>

<ul>
 <li>Its <code>Worker</code> equivalent, {{domxref("WorkerGlobalScope.self")}}.</li>
</ul>
Revert to this revision