WorkerGlobalScope
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Note: This feature is only available inWeb Workers.
TheWorkerGlobalScope interface of theWeb Workers API is an interface representing the scope of any worker. Workers have no browsing context; this scope contains the information usually conveyed byWindow objects — in this case event handlers, the console or the associatedWorkerNavigator object. EachWorkerGlobalScope has its own event loop.
This interface is usually specialized by each worker type:DedicatedWorkerGlobalScope for dedicated workers,SharedWorkerGlobalScope for shared workers, andServiceWorkerGlobalScope forServiceWorker. Theself property returns the specialized scope for each context.
In this article
Instance properties
This interface inherits properties from theEventTarget interface.
WorkerGlobalScope.cachesRead onlySecure contextReturns the
CacheStorageobject associated with the current context. This object enables functionality such as storing assets for offline use, and generating custom responses to requests.WorkerGlobalScope.crossOriginIsolatedRead onlyReturns a boolean value that indicates whether the website is in a cross-origin isolation state.
WorkerGlobalScope.cryptoRead onlyReturns the
Cryptoobject associated to the global object.WorkerGlobalScope.fontsRead onlyReturns the
FontFaceSetassociated with the worker.WorkerGlobalScope.indexedDBRead onlyProvides a mechanism for workers to asynchronously access capabilities of indexed databases; returns an
IDBFactoryobject.WorkerGlobalScope.isSecureContextRead onlyReturns a boolean indicating whether the current context is secure (
true) or not (false).WorkerGlobalScope.locationRead onlyReturns the
WorkerLocationassociated with the worker. It is a specific location object, mostly a subset of theLocationfor browsing scopes, but adapted to workers.WorkerGlobalScope.navigatorRead onlyReturns the
WorkerNavigatorassociated with the worker. It is a specific navigator object, mostly a subset of theNavigatorfor browsing scopes, but adapted to workers.WorkerGlobalScope.originRead onlyReturns the global object's origin, serialized as a string.
WorkerGlobalScope.performanceRead onlyReturns the
Performanceassociated with the worker. Only a subset of the properties and methods of thePerformanceinterface are available to workers.WorkerGlobalScope.schedulerRead onlyReturns the
Schedulerobject associated with the current context. This is the entry point for using thePrioritized Task Scheduling API.WorkerGlobalScope.trustedTypesRead onlyReturns the
TrustedTypePolicyFactoryobject associated with the global object, providing the entry point for using theTrusted Types API.WorkerGlobalScope.selfRead onlyReturns a reference to the
WorkerGlobalScopeitself. Most of the time it is a specific scope likeDedicatedWorkerGlobalScope,SharedWorkerGlobalScopeorServiceWorkerGlobalScope.
Instance methods
This interface inherits methods from theEventTarget interface.
WorkerGlobalScope.atob()Decodes a string of data which has been encoded using base-64 encoding.
WorkerGlobalScope.btoa()Creates a base-64 encodedASCII string from a string of binary data.
WorkerGlobalScope.clearInterval()Cancels the repeated execution set using
WorkerGlobalScope.setInterval().WorkerGlobalScope.clearTimeout()Cancels the delayed execution set using
WorkerGlobalScope.setTimeout().WorkerGlobalScope.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.WorkerGlobalScope.dump()DeprecatedNon-standardAllows you to write a message to stdout — i.e., in your terminal. This is the same as Firefox's
window.dump, but for workers.WorkerGlobalScope.fetch()Starts the process of fetching a resource from the network.
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');.WorkerGlobalScope.queueMicrotask()Queues a microtask to be executed at a safe time prior to control returning to the browser's event loop.
WorkerGlobalScope.setInterval()Schedules a function to execute every time a given number of milliseconds elapses.
WorkerGlobalScope.setTimeout()Schedules a function to execute in a given amount of time.
WorkerGlobalScope.structuredClone()Creates adeep clone of a given value using thestructured clone algorithm.
WorkerGlobalScope.reportError()Reports an error in a script, emulating an unhandled exception.
Events
errorFired when an error occurred.
languagechangeFired at the global/worker scope object when the user's preferred languages change.
offlineFired when the browser has lost access to the network and the value of
navigator.onLineswitched tofalse.onlineFired when the browser has gained access to the network and the value of
navigator.onLineswitched totrue.rejectionhandledFired on handled
Promiserejection events.securitypolicyviolationFired when aContent Security Policy is violated.
unhandledrejectionFired on unhandled
Promiserejection events.
Example
You won't accessWorkerGlobalScope directly in your code; however, its properties and methods are inherited by more specific global scopes such asDedicatedWorkerGlobalScope andSharedWorkerGlobalScope. For example, you could import another script into the worker and print out the contents of the worker scope'snavigator 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. fromWorkerGlobalScope, you can run lines such as those above without specifying a parent object.
Specifications
| Specification |
|---|
| HTML> # the-workerglobalscope-common-interface> |
Browser compatibility
See also
- Other global object interface:
Window,DedicatedWorkerGlobalScope,SharedWorkerGlobalScope,ServiceWorkerGlobalScope - Other Worker-related interfaces:
Worker,WorkerLocationandWorkerNavigator - Using web workers