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.
This interface inherits properties from the EventTarget
interface and WindowOrWorkerGlobalScope
and WindowEventHandlers
mixins.
WorkerGlobalScope.navigator
Read only
WorkerNavigator
associated with the worker. It is a specific navigator object, mostly a subset of the Navigator
for browsing scopes, but adapted to workers.WorkerGlobalScope.self
Read only
WorkerGlobalScope
itself. Most of the time it is a specific scope like DedicatedWorkerGlobalScope
, SharedWorkerGlobalScope
or ServiceWorkerGlobalScope
.WorkerGlobalScope.location
Read only
WorkerLocation
associated with the worker. It is a specific location object, mostly a subset of the Location
for browsing scopes, but adapted to workers.WorkerGlobalScope.performance
Read only
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
Console
associated with the worker.WindowOrWorkerGlobalScope.caches
Read only
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
IDBFactory
object.WindowOrWorkerGlobalScope.isSecureContext
Read only
true
) or not (false
).WindowOrWorkerGlobalScope.origin
Read only
WorkerGlobalScope.onerror
EventHandler
representing the code to be called when the error
event is raised.WorkerGlobalScope.onoffline
EventHandler
representing the code to be called when the offline
event is raised.WorkerGlobalScope.ononline
EventHandler
representing the code to be called when the online
event is raised.WorkerGlobalScope.onlanguagechange
EventHandler
fired at the global/worker scope object when the user's preferred languages change.WorkerGlobalScope.onclose
EventHandler
representing the code to be called when the close
event is raised.WorkerGlobalScope.onrejectionhandled
Promise
rejection events.WorkerGlobalScope.onunhandledrejection
Promise
rejection events.This interface inherits methods from the EventTarget
interface and WindowOrWorkerGlobalScope
and WindowEventHandlers
mixins.
WorkerGlobalScope.importScripts()
importScripts('foo.js', 'bar.js');
WorkerGlobalScope.dump()
window.dump
, but for workers.WindowOrWorkerGlobalScope.atob()
WindowOrWorkerGlobalScope.btoa()
WindowOrWorkerGlobalScope.clearInterval()
WindowOrWorkerGlobalScope.setInterval()
.WindowOrWorkerGlobalScope.clearTimeout()
WindowOrWorkerGlobalScope.setTimeout()
.WindowOrWorkerGlobalScope.createImageBitmap()
Promise
which resolves to an ImageBitmap
. Optionally the source is cropped to the rectangle of pixels originating at (sx, sy) with width sw, and height sh.WindowOrWorkerGlobalScope.fetch()
WindowOrWorkerGlobalScope.setInterval()
WindowOrWorkerGlobalScope.setTimeout()
WorkerGlobalScope.close()
WorkerGlobalScope
's event loop, effectively closing this particular scope. In newer browser versions, close()
is available on DedicatedWorkerGlobalScope
and SharedWorkerGlobalScope
instead. This change was made to stop close()
being available on service workers, as it isn't supposed to be used there and always throws an exception when called (see bug 1336043).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.
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'WorkerGlobalScope' in that specification. | Living Standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 4 | Yes | 3.5 | 10 | 10.6 | 4 |
location |
4 | ? | 3.5 | Yes | 11.5 | 4 |
navigator |
4 | 17 | 3.5 | Yes | 11.5 | 4 |
onerror |
4 | ? | 3.5 | Yes | 11.5 | 4 |
onlanguagechange |
4 | ? | 3.5 | Yes | 11.5 | 4 |
onoffline |
4 | ? | 29 | ? | ? | ? |
ononline |
4 | ? | 29 | ? | ? | ? |
performance |
Yes | ? | 34 | ? | ? | ? |
self |
4 | ? | 3.5 | Yes | 11.5 | 4 |
console
|
4 | ? | 29
|
Yes | Yes | Yes |
close
|
4 | ? | 3.5 | Yes | 11.5 | 4 |
dump
|
No | No | No | No | No | No |
importScripts |
Yes | Yes | Yes | Yes | Yes | Yes |
onclose
|
4 | ? | 3.5 — 50 | Yes | 11.5 | 4 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | ? | Yes | Yes | 4 | 11.5 | 5.1 | Yes |
location |
? | 40 | ? | 4 | Yes | 5.1 | 4.0 |
navigator |
? | 40 | 17 | 4 | Yes | 5.1 | 4.0 |
onerror |
? | 40 | ? | 4 | Yes | 5.1 | 4.0 |
onlanguagechange |
? | 40 | ? | 4 | Yes | 5.1 | 4.0 |
onoffline |
Yes | 40 | ? | 29 | ? | ? | 4.0 |
ononline |
Yes | 40 | ? | 29 | ? | ? | 4.0 |
performance |
? | Yes | ? | 34 | ? | ? | Yes |
self |
? | 40 | ? | 34 | Yes | 5.1 | 4.0 |
console
|
? | Yes | ? | 29
|
? | ? | Yes |
close
|
? | Yes | ? | 4 | ? | ? | Yes |
dump
|
No | No | No | 4 | No | No | No |
importScripts |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
onclose
|
? | 40 | No | 4 — 50 | Yes | 5.1 | 4.0 |
Window
, DedicatedWorkerGlobalScope
, SharedWorkerGlobalScope
, , ServiceWorkerGlobalScope
Worker
, WorkerLocation
, WorkerGlobalScope
, and ServiceWorkerGlobalScope
.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope