1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
---
title: WorkerGlobalScope
slug: Web/API/WorkerGlobalScope
tags:
- API
- Interface
- NeedsTranslation
- Reference
- TopicStub
- WorkerGlobalScope
- Workers
translation_of: Web/API/WorkerGlobalScope
---
<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 {{domxref("WindowOrWorkerGlobalScope")}} and {{domxref("WindowEventHandlers")}} mixins.</em></p>
<h3 id="Standard_properties">Standard properties</h3>
<p>{{domxref("WorkerGlobalScope.navigator")}} {{readOnlyinline}}</p>
<dl>
<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")}}, {{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="Properties_implemented_from_elsewhere">Properties implemented from elsewhere</h3>
<dl>
<dt>{{domxref("WindowOrWorkerGlobalScope.caches")}} {{readOnlyinline}}</dt>
<dd>Returns the {{domxref("CacheStorage")}} object associated with the current context. This object enables functionality such as storing assets for offline use, and generating custom responses to requests.</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.indexedDB")}} {{readonlyInline}}</dt>
<dd>Provides a mechanism for applications to asynchronously access capabilities of indexed databases; returns an {{domxref("IDBFactory")}} object.</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.isSecureContext")}} {{readOnlyinline}}</dt>
<dd>Returns a boolean indicating whether the current context is secure (<code>true</code>) or not (<code>false</code>).</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.origin")}} {{readOnlyinline}}</dt>
<dd>Returns the global object's origin, serialized as a string. (This does not yet appear to be implemented in any browser.)</dd>
</dl>
<h2 id="Events">Events</h2>
<dl>
<dt><code>error</code></dt>
<dd>Fired when an error occured.<br>
Also available via the {{domxref("WorkerGlobalScope.onerror")}} property.</dd>
<dt><code>offline</code></dt>
<dd>Fired when the browser has lost access to the network and the value of <code>navigator.onLine</code> switched to <code>false</code>.<br>
Also available via the {{domxref("WorkerGlobalScope.onoffline")}} property.</dd>
<dt><code>online</code></dt>
<dd>Fired when the browser has gained access to the network and the value of <code>navigator.onLine</code> switched to <code>true</code>.<br>
Also available via the {{domxref("WorkerGlobalScope.ononline")}} property.</dd>
<dt><code><a href="/en-US/docs/Web/API/WorkerGlobalScope/languagechange_event">languagechange</a></code></dt>
<dd>Fired at the global/worker scope object when the user's preferred languages change.<br>
Also available via the {{domxref("WorkerGlobalScope.onlanguagechange")}} property.</dd>
</dl>
<dl>
<dt><code>close</code> {{non-standard_inline}} {{obsolete_inline}}</dt>
<dd>Is an {{event("Event_handlers")}} representing the code to be called when the {{event("close")}} event is raised.<br>
Also available via the {{domxref("WorkerGlobalScope.onclose")}} property.</dd>
<dt><code>rejectionhandled</code> {{non-standard_inline}}</dt>
<dd>An event handler for handled <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" title="The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future."><code>Promise</code></a> rejection events.<br>
Also available via the {{domxref("WorkerGlobalScope.onrejectionhandled")}} property.</dd>
<dt><code>unhandledrejection</code> {{non-standard_inline}}</dt>
<dd>An event handler for unhandled <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" title="The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future."><code>Promise</code></a> rejection events.<br>
Also available via the {{domxref("WorkerGlobalScope.onunhandledrejection")}} property.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<p><em>This interface inherits methods from the {{domxref("EventTarget")}} interface and {{domxref("WindowOrWorkerGlobalScope")}} and {{domxref("WindowEventHandlers")}} mixins.</em></p>
<h3 id="Standard_methods">Standard methods</h3>
<dl>
<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("WindowOrWorkerGlobalScope.atob()")}}</dt>
<dd>Decodes a string of data which has been encoded using base-64 encoding.</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.btoa()")}}</dt>
<dd>Creates a base-64 encoded ASCII string from a string of binary data.</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.clearInterval()")}}</dt>
<dd>Cancels the repeated execution set using {{domxref("WindowOrWorkerGlobalScope.setInterval()")}}.</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.clearTimeout()")}}</dt>
<dd>Cancels the delayed execution set using {{domxref("WindowOrWorkerGlobalScope.setTimeout()")}}.</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.createImageBitmap()")}}</dt>
<dd>Accepts a variety of different image sources, and returns a {{domxref("Promise")}} which resolves to an {{domxref("ImageBitmap")}}. Optionally the source is cropped to the rectangle of pixels originating at <em>(sx, sy)</em> with width sw, and height sh.</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.fetch()")}}</dt>
<dd>Starts the process of fetching a resource from the network.</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.setInterval()")}}</dt>
<dd>Schedules a function to execute every time a given number of milliseconds elapses.</dd>
<dt>{{domxref("WindowOrWorkerGlobalScope.setTimeout()")}}</dt>
<dd>Schedules a function to execute in a given amount of time.</dd>
</dl>
<h3 id="Deprecated_methods">Deprecated 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. In newer browser versions, <code>close()</code> is available on <code>DedicatedWorkerGlobalScope</code> and {{domxref("SharedWorkerGlobalScope.close", "SharedWorkerGlobalScope")}} instead. This change was made to stop <code>close()</code> being available on service workers, as it isn't supposed to be used there and always throws an exception when called (see {{bug(1336043)}}).</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>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></td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.WorkerGlobalScope")}}</p>
<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">Using web workers.</a></li>
</ul>
|