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.navigator
Read only
- Returns the
WorkerNavigator
associated with the worker. It is a specific navigator object, mostly a subset of theNavigator
for browsing scopes, but adapted to workers. WorkerGlobalScope.self
Read only- Returns a reference to the
WorkerGlobalScope
itself. Most of the time it is a specific scope likeDedicatedWorkerGlobalScope
,SharedWorkerGlobalScope
orServiceWorkerGlobalScope
. WorkerGlobalScope.location
Read only- Returns the
WorkerLocation
associated with the worker. It is a specific location object, mostly a subset of theLocation
for browsing scopes, but adapted to workers.
Non-standard properties
WorkerGlobalScope.performance
Read only- Returns the
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. WorkerGlobalScope.console
Read only- Returns the
Console
associated with the worker.
Properties implemented from elsewhere
WindowOrWorkerGlobalScope.caches
Read only- Returns the
CacheStorage
object associated with the current context. This object enables functionality such as storing assets for offline use, and generating custom responses to requests. WindowOrWorkerGlobalScope.indexedDB
Read only- Provides a mechanism for applications to asynchronously access capabilities of indexed databases; returns an
IDBFactory
object. WindowOrWorkerGlobalScope.isSecureContext
Read only- Returns a boolean indicating whether the current context is secure (
true
) or not (false
). WindowOrWorkerGlobalScope.origin
Read 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 occured.
Also available via theWorkerGlobalScope.onerror
property. offline
- Fired when the browser has lost access to the network and the value of
navigator.onLine
switched tofalse
.
Also available via theWorkerGlobalScope.onoffline
property. online
- Fired when the browser has gained access to the network and the value of
navigator.onLine
switched totrue
.
Also available via theWorkerGlobalScope.ononline
property. languagechange
- Fired at the global/worker scope object when the user's preferred languages change.
Also available via theWorkerGlobalScope.onlanguagechange
property.
close
- Is an
EventHandler
representing the code to be called when theclose
event is raised.
Also available via theWorkerGlobalScope.onclose
property. rejectionhandled
- An event handler for handled
Promise
rejection events.
Also available via theWorkerGlobalScope.onrejectionhandled
property. unhandledrejection
- An event handler for unhandled
Promise
rejection events.
Also available via theWorkerGlobalScope.onunhandledrejection
property.
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
WindowOrWorkerGlobalScope.atob()
- Decodes a string of data which has been encoded using base-64 encoding.
WindowOrWorkerGlobalScope.btoa()
- Creates a base-64 encoded ASCII string from a string of binary data.
WindowOrWorkerGlobalScope.clearInterval()
- Cancels the repeated execution set using
WindowOrWorkerGlobalScope.setInterval()
. WindowOrWorkerGlobalScope.clearTimeout()
- Cancels the delayed execution set using
WindowOrWorkerGlobalScope.setTimeout()
. WindowOrWorkerGlobalScope.createImageBitmap()
- Accepts a variety of different image sources, and returns a
Promise
which resolves to anImageBitmap
. Optionally the source is cropped to the rectangle of pixels originating at (sx, sy) with width sw, and height sh. WindowOrWorkerGlobalScope.fetch()
- Starts the process of fetching a resource from the network.
WindowOrWorkerGlobalScope.setInterval()
- Schedules a function to execute every time a given number of milliseconds elapses.
WindowOrWorkerGlobalScope.setTimeout()
- Schedules a function to execute in a given amount of time.
Deprecated methods
WorkerGlobalScope.close()
- Discards any tasks queued in the
WorkerGlobalScope
's event loop, effectively closing this particular scope. In newer browser versions,close()
is available onDedicatedWorkerGlobalScope
andSharedWorkerGlobalScope
instead. This change was made to stopclose()
being available on service workers, as it isn't supposed to be used there and always throws an exception when called (see bug 1336043).
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);
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
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'WorkerGlobalScope' in that specification. |
Living Standard |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
WorkerGlobalScope | Chrome Full support 4 | Edge Full support 12 | Firefox Full support 3.5 | IE Full support 10 | Opera Full support 10.6 | Safari Full support 4 | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 4 | Opera Android Full support 11 | Safari iOS Full support 3.2 | Samsung Internet Android Full support Yes |
close | Chrome Full support 4 | Edge Full support 12 | Firefox Full support 3.5 | IE Full support Yes | Opera Full support 11.5 | Safari Full support 4 | WebView Android Full support 37 | Chrome Android Full support Yes | Firefox Android Full support 4 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
console | Chrome Full support 4 | Edge Full support 12 | Firefox
Full support
29
| IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 37 | Chrome Android Full support Yes | Firefox Android
Full support
29
| Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
dump | Chrome No support No | Edge No support No | Firefox No support No | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No |
importScripts | Chrome Full support 4 | Edge Full support 12 | Firefox Full support 4 | IE Full support 10 | Opera Full support 10.6 | Safari Full support 4 | WebView Android Full support ≤37 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 11 | Safari iOS Full support 3.2 | Samsung Internet Android Full support 1.0 |
languagechange event | Chrome Full support 4 | Edge Full support 12 | Firefox Full support 74 | IE Full support Yes | Opera Full support 11.5 | Safari Full support 4 | WebView Android ? | Chrome Android Full support 40 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS Full support 5.1 | Samsung Internet Android Full support 4.0 |
location | Chrome Full support 4 | Edge Full support 12 | Firefox Full support 3.5 | IE Full support Yes | Opera Full support 11.5 | Safari Full support 4 | WebView Android Full support 37 | Chrome Android Full support 40 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 5.1 | Samsung Internet Android Full support 4.0 |
navigator | Chrome Full support 4 | Edge Full support 17 | Firefox Full support 3.5 | IE Full support Yes | Opera Full support 11.5 | Safari Full support 4 | WebView Android Full support 37 | Chrome Android Full support 40 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 5.1 | Samsung Internet Android Full support 4.0 |
onclose | Chrome Full support 4 | Edge Full support 12 | Firefox No support 3.5 — 50 | IE Full support Yes | Opera Full support 11.5 | Safari Full support 4 | WebView Android Full support 37 | Chrome Android Full support 40 | Firefox Android No support 4 — 50 | Opera Android Full support Yes | Safari iOS Full support 5.1 | Samsung Internet Android Full support 4.0 |
onerror | Chrome Full support 4 | Edge Full support 12 | Firefox Full support 3.5 | IE Full support Yes | Opera Full support 11.5 | Safari Full support 4 | WebView Android Full support 37 | Chrome Android Full support 40 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 5.1 | Samsung Internet Android Full support 4.0 |
onlanguagechange | Chrome Full support 4 | Edge Full support 12 | Firefox Full support 74 | IE Full support Yes | Opera Full support 11.5 | Safari Full support 4 | WebView Android Full support 37 | Chrome Android Full support 40 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS Full support 5.1 | Samsung Internet Android Full support 4.0 |
onoffline | Chrome Full support 4 | Edge Full support ≤79 | Firefox Full support 29 | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support 40 | Firefox Android Full support 29 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support 4.0 |
ononline | Chrome Full support 4 | Edge Full support ≤79 | Firefox Full support 29 | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support 40 | Firefox Android Full support 29 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support 4.0 |
performance | Chrome Full support Yes | Edge Full support ≤79 | Firefox Full support 34 | IE ? | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 34 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
self | Chrome Full support 4 | Edge Full support 12 | Firefox Full support 3.5 | IE Full support Yes | Opera Full support 11.5 | Safari Full support 4 | WebView Android Full support 37 | Chrome Android Full support 40 | Firefox Android Full support 34 | Opera Android Full support Yes | Safari iOS Full support 5.1 | Samsung Internet Android Full support 4.0 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.
- See implementation notes.
- See implementation notes.
See also
- Other global object interface:
Window
,DedicatedWorkerGlobalScope
,SharedWorkerGlobalScope
, ,ServiceWorkerGlobalScope
- Other Worker-related interfaces:
Worker
,WorkerLocation
,WorkerGlobalScope
, andServiceWorkerGlobalScope
. - Using web workers.