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 753461 of WorkerGlobalScope

  • Revision slug: Web/API/WorkerGlobalScope
  • Revision title: WorkerGlobalScope
  • Revision id: 753461
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment Mountain View APIRef Project

Revision Content

{{APIRef("Web Workers API")}}

The WorkerGlobalScope interface of the Web Workers API is an interface representing the scope of any worker. Workers have no browsing context; this scope contains the information usually conveyed by {{domxref("Window")}} objects — in this case event handlers, the console or the associated {{domxref("WorkerNavigator")}} object. Each WorkerGlobalScope has its own event loop.

This interface is usually specialized by each worker type: {{domxref("DedicatedWorkerGlobalScope")}} for dedicated workers, {{domxref("SharedWorkerGlobalScope")}} for shared workers, and {{domxref("ServiceWorkerGlobalScope")}} for ServiceWorker. The self property returns the specialized scope for each context.

Properties

This interface inherits properties from the {{domxref("EventTarget")}} interface and implements properties from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, and {{domxref("WindowEventHandlers")}}.

Standard properties

{{domxref("WorkerGlobalScope.navigator")}} {{readOnlyinline}}
Returns the {{domxref("WorkerNavigator")}} associated with the worker. It is a specific navigator object, mostly a subset of the {{domxref("Navigator")}} for browsing scopes, but adapted to workers.
{{domxref("WorkerGlobalScope.self")}} {{readOnlyinline}}
Returns a reference to the WorkerGlobalScope itself. Most of the time it is a specific scope like {{domxref("DedicatedWorkerGlobalScope")}},  {{domxref("SharedWorkerGlobalScope")}} or {{domxref("ServiceWorkerGlobalScope")}}.
{{domxref("WorkerGlobalScope.location")}} {{readOnlyinline}}
Returns the {{domxref("WorkerLocation")}} associated with the worker. It is a specific location object, mostly a subset of the {{domxref("Location")}} for browsing scopes, but adapted to workers.

Non-standard properties

{{domxref("WorkerGlobalScope.performance")}} {{readOnlyinline}} {{Non-standard_inline}}
Returns the {{domxref("Performance")}} associated with the worker. It is a regular performance object, except that only a subset of its property and methods are available to workers.
{{domxref("WorkerGlobalScope.console")}} {{readOnlyinline}} {{Non-standard_inline}}
Returns the {{domxref("Console")}} associated with the worker.

Event Handlers

This interface inherits event handlers from the {{domxref("EventTarget")}} interface and implements event handlers from {{domxref("WindowTimers")}}, and {{domxref("WindowBase64")}}.

{{domxref("WorkerGlobalScope.onerror")}}
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("error")}} event is raised.
{{domxref("WorkerGlobalScope.onoffline")}}
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("offline")}} event is raised.
{{domxref("WorkerGlobalScope.ononline")}}
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("online")}} event is raised.
{{domxref("WorkerGlobalScope.onlanguagechange")}}
An {{domxref("EventHandler")}} fired at the global/worker scope object when the user's preferred languages change.
{{domxref("WorkerGlobalScope.onclose")}} {{Non-standard_inline}}
Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("close")}} event is raised.

Methods

This interface inherits methods from the {{domxref("EventTarget")}} interface and implements methods from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, and {{domxref("WindowEventHandlers")}}.

Standard methods

{{domxref("WorkerGlobalScope.close()")}}
Discards any tasks queued in the WorkerGlobalScope's event loop, effectively closing this particular scope.
{{domxref("WorkerGlobalScope.importScripts()")}}
Imports one or more scripts into the worker's scope. You can specify as many as you'd like, separated by commas. For example: importScripts('foo.js', 'bar.js');

Non-standard methods

{{domxref("WorkerGlobalScope.dump()")}} {{non-standard_inline}}
Allows you to write a message to stdout — i.e. in your terminal. This is the same as Firefox's {{domxref("window.dump")}}, but for workers.

Methods implemented from elsewhere

{{domxref("WindowBase64.atob()")}}
Decodes a string of data which has been encoded using base-64 encoding.
{{domxref("WindowBase64.btoa()")}}
Creates a base-64 encoded ASCII string from a string of binary data.
{{domxref("WindowTimers.clearInterval()")}}
Cancels the repeated execution set using {{domxref("WindowTimers.setInterval()")}}.
{{domxref("WindowTimers.clearTimeout()")}}
Cancels the repeated execution set using {{domxref("WindowTimers.setTimeout()")}}.
{{domxref("WindowTimers.setInterval()")}}
Schedules the execution of a function each X milliseconds.
{{domxref("WindowTimers.setTimeout()")}}
Sets a delay for executing a function.

Example

You won't access WorkerGlobalScope directly in your code; however, its properties and methods are inherited by more specific global scopes such as {{domxref("DedicatedWorkerGlobalScope")}} and {{domxref("SharedWorkerGlobalScope")}}. For example, you could import another script into the worker and print out the contents of the worker scope's navigator object using the following two lines:

importScripts('foo.js');
console.log(navigator);

Note: Since the global scope of the worker script is effectively the global scope of the worker you are running ({{domxref("DedicatedWorkerGlobalScope")}} or whatever) and all worker global scopes inherit methods, properties, etc. from WorkerGlobalScope, you can run lines such as those above without specifying a parent object.

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', '#workerglobalscope', 'WorkerGlobalScope')}} {{Spec2('HTML WHATWG')}} No change from {{SpecName("Web Workers")}}.
{{SpecName('Web Workers', '#workerglobalscope', 'WorkerGlobalScope')}} {{Spec2('Web Workers')}} Initial definition.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(4)}} {{CompatGeckoDesktop("1.9.1")}} 10 10.6 4
ononline, onoffline {{CompatVersionUnknown}} {{CompatGeckoDesktop("29")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
console {{Non-standard_inline}} {{CompatVersionUnknown}} {{CompatGeckoDesktop("29")}} as  WorkerConsole
{{CompatGeckoDesktop("30")}} as the regular Console
{{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
performance {{CompatVersionUnknown}} {{CompatGeckoDesktop("34")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("1.9.1")}} 1.0.1 10 11.5 5.1
ononline, onoffline {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("29")}} as a specific WorkerConsole
{{CompatGeckoMobile("30")}} as the regular Console
1.4 {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
console {{Non-standard_inline}} {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("29")}} 1.4 {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
performance {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("34")}} 2.1 {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

  • Other global object interface: {{domxref("Window")}}, {{domxref("DedicatedWorkerGlobalScope")}}, {{domxref("SharedWorkerGlobalScope")}}, , {{domxref("ServiceWorkerGlobalScope")}}
  • Other Worker-related interfaces: {{domxref("Worker")}}, {{domxref("WorkerLocation")}}, {{domxref("WorkerGlobalScope")}}, and {{domxref("ServiceWorkerGlobalScope")}}.
  • Using web workers.

Revision Source

<p>{{APIRef("Web Workers API")}}</p>

<p>The <code><strong>WorkerGlobalScope</strong></code> interface of the <a href="/en-US/docs/Web/API/Web_Workers_API">Web Workers API</a> is an interface representing the scope of any worker. Workers have no browsing context; this scope contains the information usually conveyed by {{domxref("Window")}} objects — in this case event handlers, the console or the associated {{domxref("WorkerNavigator")}} object. Each <code>WorkerGlobalScope</code> has its own event loop.</p>

<p>This interface is usually specialized by each worker type: {{domxref("DedicatedWorkerGlobalScope")}} for dedicated workers, {{domxref("SharedWorkerGlobalScope")}} for shared workers, and {{domxref("ServiceWorkerGlobalScope")}} for <a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker</a>. The <code>self</code> property returns the specialized scope for each context.</p>

<h2 id="Properties">Properties</h2>

<p><em>This interface inherits properties from the {{domxref("EventTarget")}} interface and implements properties from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, and {{domxref("WindowEventHandlers")}}.</em></p>

<h3 id="Standard_properties">Standard properties</h3>

<dl>
 <dt>{{domxref("WorkerGlobalScope.navigator")}} {{readOnlyinline}}</dt>
 <dd>Returns the {{domxref("WorkerNavigator")}} associated with the worker. It is a specific navigator object, mostly a subset of the {{domxref("Navigator")}} for browsing scopes, but adapted to workers.</dd>
 <dt>{{domxref("WorkerGlobalScope.self")}} {{readOnlyinline}}</dt>
 <dd>Returns a reference to the <code>WorkerGlobalScope</code> itself. Most of the time it is a specific scope like {{domxref("DedicatedWorkerGlobalScope")}},&nbsp; {{domxref("SharedWorkerGlobalScope")}} or {{domxref("ServiceWorkerGlobalScope")}}.</dd>
 <dt>{{domxref("WorkerGlobalScope.location")}} {{readOnlyinline}}</dt>
 <dd>Returns the {{domxref("WorkerLocation")}} associated with the worker. It is a specific location object, mostly a subset of the {{domxref("Location")}} for browsing scopes, but adapted to workers.</dd>
</dl>

<h3 id="Non-standard_properties">Non-standard properties</h3>

<dl>
 <dt>{{domxref("WorkerGlobalScope.performance")}} {{readOnlyinline}} {{Non-standard_inline}}</dt>
 <dd>Returns the {{domxref("Performance")}} associated with the worker. It is a regular performance object, except that only a subset of its property and methods are available to workers.</dd>
 <dt>{{domxref("WorkerGlobalScope.console")}} {{readOnlyinline}} {{Non-standard_inline}}</dt>
 <dd>Returns the {{domxref("Console")}} associated with the worker.</dd>
</dl>

<h3 id="Event_Handlers">Event Handlers</h3>

<p><em>This interface inherits event handlers from the {{domxref("EventTarget")}} interface and implements event handlers from {{domxref("WindowTimers")}}, and {{domxref("WindowBase64")}}.</em></p>

<dl>
 <dt>{{domxref("WorkerGlobalScope.onerror")}}</dt>
 <dd>Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("error")}} event is raised.</dd>
 <dt>{{domxref("WorkerGlobalScope.onoffline")}}</dt>
 <dd>Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("offline")}} event is raised.</dd>
 <dt>{{domxref("WorkerGlobalScope.ononline")}}</dt>
 <dd>Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("online")}} event is raised.</dd>
 <dt>{{domxref("WorkerGlobalScope.onlanguagechange")}}</dt>
 <dd>An {{domxref("EventHandler")}} fired at the global/worker scope object when the user's preferred languages change.</dd>
</dl>

<dl>
 <dt>{{domxref("WorkerGlobalScope.onclose")}} {{Non-standard_inline}}</dt>
 <dd>Is an {{domxref("EventHandler")}} representing the code to be called when the {{event("close")}} event is raised.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<p><em>This interface inherits methods from the {{domxref("EventTarget")}} interface and implements methods from {{domxref("WindowTimers")}}, {{domxref("WindowBase64")}}, and {{domxref("WindowEventHandlers")}}.</em></p>

<h3 id="Standard_methods">Standard methods</h3>

<dl>
 <dt>{{domxref("WorkerGlobalScope.close()")}}</dt>
 <dd>Discards any tasks queued in the <code>WorkerGlobalScope</code>'s event loop, effectively closing this particular scope.</dd>
 <dt>{{domxref("WorkerGlobalScope.importScripts()")}}</dt>
 <dd>Imports one or more scripts into the worker's scope. You can specify as many as you'd like, separated by commas. For example:<code> importScripts('foo.js', 'bar.js');</code></dd>
</dl>

<h3 id="Non-standard_methods">Non-standard methods</h3>

<dl>
 <dt>{{domxref("WorkerGlobalScope.dump()")}} {{non-standard_inline}}</dt>
 <dd>Allows you to write a message to stdout — i.e. in your terminal. This is the same as Firefox's {{domxref("window.dump")}}, but for workers.</dd>
</dl>

<h3 id="Methods_implemented_from_elsewhere">Methods implemented from elsewhere</h3>

<dl>
 <dt>{{domxref("WindowBase64.atob()")}}</dt>
 <dd>Decodes a string of data which has been encoded using base-64 encoding.</dd>
 <dt>{{domxref("WindowBase64.btoa()")}}</dt>
 <dd>Creates a base-64 encoded ASCII string from a string of binary data.</dd>
 <dt>{{domxref("WindowTimers.clearInterval()")}}</dt>
 <dd>Cancels the repeated execution set using {{domxref("WindowTimers.setInterval()")}}.</dd>
 <dt>{{domxref("WindowTimers.clearTimeout()")}}</dt>
 <dd>Cancels the repeated execution set using {{domxref("WindowTimers.setTimeout()")}}.</dd>
 <dt>{{domxref("WindowTimers.setInterval()")}}</dt>
 <dd>Schedules the execution of a function each X milliseconds.</dd>
 <dt>{{domxref("WindowTimers.setTimeout()")}}</dt>
 <dd>Sets a delay for executing a function.</dd>
</dl>

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

<p>You won't access <code>WorkerGlobalScope</code> directly in your code; however, its properties and methods are inherited by more specific global scopes such as {{domxref("DedicatedWorkerGlobalScope")}} and {{domxref("SharedWorkerGlobalScope")}}. For example, you could import another script into the worker and print out the contents of the worker scope's <code>navigator</code> object using the following two lines:</p>

<pre class="brush: js">
importScripts('foo.js');
console.log(navigator);</pre>

<div class="note">
<p><strong>Note</strong>: Since the global scope of the worker script is effectively the global scope of the worker you are running ({{domxref("DedicatedWorkerGlobalScope")}} or whatever) and all worker global scopes inherit methods, properties, etc. from <code>WorkerGlobalScope</code>, you can run lines such as those above without specifying a parent object.</p>
</div>

<h2 id="Specifications">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', '#workerglobalscope', 'WorkerGlobalScope')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>No change from {{SpecName("Web Workers")}}.</td>
  </tr>
  <tr>
   <td>{{SpecName('Web Workers', '#workerglobalscope', 'WorkerGlobalScope')}}</td>
   <td>{{Spec2('Web Workers')}}</td>
   <td>Initial definition.</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>{{CompatChrome(4)}}</td>
   <td>{{CompatGeckoDesktop("1.9.1")}}</td>
   <td>10</td>
   <td>10.6</td>
   <td>4</td>
  </tr>
  <tr>
   <td><code>ononline</code>, <code>onoffline</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("29")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>console</code> {{Non-standard_inline}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("29")}} as&nbsp; <code>WorkerConsole</code><br />
    {{CompatGeckoDesktop("30")}} as the regular <code>Console</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>performance</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("34")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</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>Firefox OS (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("1.9.1")}}</td>
   <td>1.0.1</td>
   <td>10</td>
   <td>11.5</td>
   <td>5.1</td>
  </tr>
  <tr>
   <td><code>ononline</code>, <code>onoffline</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("29")}} as a specific <code>WorkerConsole</code><br />
    {{CompatGeckoMobile("30")}} as the regular <code>Console</code></td>
   <td>1.4</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>console</code> {{Non-standard_inline}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("29")}}</td>
   <td>1.4</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>performance</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("34")}}</td>
   <td>2.1</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

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

<ul>
 <li>Other global object interface: {{domxref("Window")}}, {{domxref("DedicatedWorkerGlobalScope")}}, {{domxref("SharedWorkerGlobalScope")}}, , {{domxref("ServiceWorkerGlobalScope")}}</li>
 <li>Other Worker-related interfaces: {{domxref("Worker")}}, {{domxref("WorkerLocation")}}, {{domxref("WorkerGlobalScope")}}, and {{domxref("ServiceWorkerGlobalScope")}}.</li>
 <li><a href="/en-US/docs/Web/Guide/Performance/Using_web_workers" title="/en-US/docs/Web/Guide/Performance/Using_web_workers">Using web workers.</a></li>
</ul>

<dl>
</dl>
Revert to this revision