WorkerGlobalScope
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 Window objects — in this case event handlers, the console or the associated WorkerNavigator object. Each WorkerGlobalScope has its own event loop.
This interface is usually specialized by each worker type: DedicatedWorkerGlobalScope for dedicated workers, SharedWorkerGlobalScope for shared workers, and ServiceWorkerGlobalScope for ServiceWorker. The self property returns the specialized scope for each context.
Properties
This interface inherits properties from the EventTarget interface and WindowOrWorkerGlobalScope and WindowEventHandlers mixins.
Standard properties
-
WorkerGlobalScope.navigatorRead only -
Returns the
WorkerNavigatorassociated with the worker. It is a specific navigator object, mostly a subset of theNavigatorfor browsing scopes, but adapted to workers. -
WorkerGlobalScope.selfRead only -
Returns a reference to the
WorkerGlobalScopeitself. Most of the time it is a specific scope likeDedicatedWorkerGlobalScope,SharedWorkerGlobalScopeorServiceWorkerGlobalScope. -
WorkerGlobalScope.locationRead only -
Returns the
WorkerLocationassociated with the worker. It is a specific location object, mostly a subset of theLocationfor browsing scopes, but adapted to workers.
Non-standard properties
-
WorkerGlobalScope.performanceRead only -
Returns the
Performanceassociated with the worker. It is a regular performance object, except that only a subset of its property and methods are available to workers. -
WorkerGlobalScope.consoleRead only -
Returns the
consoleassociated with the worker.
Properties implemented from elsewhere
-
cachesRead only -
Returns the
CacheStorageobject associated with the current context. This object enables functionality such as storing assets for offline use, and generating custom responses to requests. -
indexedDBRead only -
Provides a mechanism for applications to asynchronously access capabilities of indexed databases; returns an
IDBFactoryobject. -
isSecureContextRead only -
Returns a boolean indicating whether the current context is secure (
true) or not (false). -
originRead only -
Returns the global object's origin, serialized as a string. (This does not yet appear to be implemented in any browser.)
Events
error-
Fired when an error occurred. Also available via the
WorkerGlobalScope.onerrorproperty. offline-
Fired when the browser has lost access to the network and the value of
navigator.onLineswitched tofalse. Also available via theWorkerGlobalScope.onofflineproperty. online-
Fired when the browser has gained access to the network and the value of
navigator.onLineswitched totrue. Also available via theWorkerGlobalScope.ononlineproperty. languagechange-
Fired at the global/worker scope object when the user's preferred languages change. Also available via the
WorkerGlobalScope.onlanguagechangeproperty. -
rejectionhandled -
An event handler for handled
Promiserejection events. Also available via theWorkerGlobalScope.onrejectionhandledproperty. -
unhandledrejection -
An event handler for unhandled
Promiserejection events. Also available via theWorkerGlobalScope.onunhandledrejectionproperty.
Methods
This interface inherits methods from the EventTarget interface and WindowOrWorkerGlobalScope and WindowEventHandlers mixins.
Standard methods
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
-
WorkerGlobalScope.dump() -
Allows you to write a message to stdout — i.e. in your terminal. This is the same as Firefox's
window.dump, but for workers.
Methods implemented from elsewhere
atob()-
Decodes a string of data which has been encoded using base-64 encoding.
btoa()-
Creates a base-64 encoded ASCII string from a string of binary data.
clearInterval()-
Cancels the repeated execution set using
setInterval(). clearTimeout()-
Cancels the delayed execution set using
setTimeout(). createImageBitmap()-
Accepts a variety of different image sources, and returns a
Promisewhich resolves to anImageBitmap. Optionally the source is cropped to the rectangle of pixels originating at (sx, sy) with width sw, and height sh. fetch()-
Starts the process of fetching a resource from the network.
setInterval()-
Schedules a function to execute every time a given number of milliseconds elapses.
setTimeout()-
Schedules a function to execute in a given amount of time.
reportError()-
Reports an error in a script, emulating an unhandled exception.
Example
You won't access WorkerGlobalScope directly in your code; however, its properties and methods are inherited by more specific global scopes such as DedicatedWorkerGlobalScope and 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 (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
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
WorkerGlobalScope |
4 |
12 |
3.5 |
10 |
10.6 |
4 |
≤37 |
Yes |
4 |
11 |
5 |
Yes |
dump |
No |
No |
No |
No |
No |
No |
No |
No |
4 |
No |
No |
No |
importScripts |
4 |
12 |
4 |
10 |
10.6 |
4 |
≤37 |
18 |
4 |
11 |
5 |
1.0 |
languagechange_event |
4 |
12 |
74 |
Yes |
11.5 |
4 |
≤37 |
40 |
No |
Yes |
5 |
4.0 |
location |
4 |
12 |
3.5 |
10 |
11.5 |
4 |
37 |
40 |
4 |
Yes |
5 |
4.0 |
navigator |
4 |
17 |
3.5 |
10 |
11.5 |
4 |
37 |
40 |
4 |
Yes |
5 |
4.0 |
onerror |
4 |
12 |
3.5 |
10 |
11.5 |
4 |
37 |
40 |
4 |
Yes |
5 |
4.0 |
onlanguagechange |
4 |
12 |
74 |
Yes |
11.5 |
4 |
37 |
40 |
No |
Yes |
5 |
4.0 |
onoffline |
4 |
≤79 |
29 |
No |
? |
8 |
40 |
40 |
29 |
? |
8 |
4.0 |
ononline |
4 |
≤79 |
29 |
No |
? |
8 |
40 |
40 |
29 |
? |
8 |
4.0 |
self |
4 |
12 |
3.5 |
10 |
11.5 |
4 |
37 |
40 |
34 |
Yes |
5 |
4.0 |
See also
- Other global object interface:
Window,DedicatedWorkerGlobalScope,SharedWorkerGlobalScope, ,ServiceWorkerGlobalScope - Other Worker-related interfaces:
Worker,WorkerLocation,WorkerGlobalScope, andServiceWorkerGlobalScope. - Using web workers.
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope