SharedWorker: SharedWorker() constructor

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Warning: This script passed to the url element is executed. APIs like this are known as injection sinks, and are potentially a vector for cross-site scripting (XSS) attacks.

You can mitigate this risk by having a Content Security Policy (CSP) that restricts the locations from which scripts can be loaded, and by always assigning TrustedScriptURL objects instead of strings and enforcing trusted types. See Security considerations in the Worker() constructor for more information.

The SharedWorker() constructor creates a SharedWorker object that executes the script at the specified URL.

Syntax

js
new SharedWorker(url)
new SharedWorker(url, name)
new SharedWorker(url, options)

Parameters

url

A TrustedScriptURL object or a string representing the URL of the script or module that the worker will execute. This must be same-origin with the caller's document, or a blob: or data: URL. The URL is resolved relative to the current HTML page's location.

name Optional

A string specifying an identifying name for the SharedWorkerGlobalScope representing the scope of the worker, which is useful for creating new instances of the same SharedWorker and debugging.

options Optional

An object containing option properties that can set when creating the object instance. Available properties are as follows:

type

A string specifying the type of worker to create. The value can be classic or module. The default used is classic.

credentials

A string specifying whether the browser sends credentials when importing modules into a module worker. The allowed values are the same as can be passed to the fetch() request: omit, same-origin, or include. The default is same-origin (only include credentials for same-origin requests).

This is ignored for classic workers.

name

A string specifying an identifying name for the SharedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.

sameSiteCookies

A string indicating which SameSite cookies should be available to the worker. Can have one of the following two values:

'all'

SameSite=Strict, SameSite=Lax, and SameSite=None cookies will all be available to the worker. This option is only supported in first-party contexts, and is the default in first-party contexts.

'none'

Only SameSite=None cookies will be available to the worker. This option is supported in first-party and third-party contexts, and is the default in third-party contexts.

Exceptions

SecurityError DOMException

Thrown if the document is not allowed to start workers, for example if the URL has an invalid syntax or if the same-origin policy is violated, or if the sameSiteCookies value is not supported in the given context.

NetworkError DOMException

Thrown if the MIME type of the worker script is incorrect. It should always be text/javascript (for historical reasons other JavaScript MIME types may be accepted).

SyntaxError DOMException

Thrown if url cannot be parsed.

TypeError

Thrown if the url parameter is a string when Trusted Types are enforced by a CSP and no default policy is defined.

Description

The SharedWorker() constructor creates a SharedWorker object that executes the classic script or module at the specified URL.

The script must be same-origin with the associated document, but may itself import scripts or modules that are cross-origin (if permitted by CORS and other restrictions). If a cross-origin worker is required, users must load it from an intermediate same-origin worker or a blob.

For more information see Description in the Worker() constructor.

Examples

For brevity, the following examples don't use Trusted Types. In production your code should always use trusted types when passing data originating from users into injection sinks.

For an example, see Using Trusted Types in the Worker() constructor examples.

Basic usage

The following code snippet shows creation of a SharedWorker object using the SharedWorker() constructor and subsequent usage of the object:

js
const myWorker = new SharedWorker("worker.js");

myWorker.port.start();

[first, second].forEach((input) => {
  input.onchange = () => {
    myWorker.port.postMessage([first.value, second.value]);
    console.log("Message posted to worker");
  };
});

myWorker.port.onmessage = (e) => {
  result1.textContent = e.data;
  console.log("Message received from worker");
};

For a full example, see our Basic shared worker example (run shared worker.)

Specifications

Specification
HTML
# dom-sharedworker-dev

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
SharedWorker() constructor
Chrome – Full support
Chrome 5 (Release date: 2010-05-25)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 29 (Release date: 2014-04-29)
footnote Full support
Opera – Full support
Opera 10.6 (Release date: 2010-07-01)
footnote Full support
Safari – No support
Safari 5 – 6 (Release date: 2010-06-07)
footnote Removed in 7 and later
Safari – Full support
Safari 16 (Release date: 2022-09-12)
footnote Full support
Chrome Android – Preview support
Chrome Android 148 (Release date: 2026-05-05)
footnote Preview browser support
Firefox for Android – Full support
Firefox for Android 33 (Release date: 2014-10-14)
footnote Full support
Opera Android – No support
Opera Android 11 – 12.1 (Release date: 2011-03-22)
footnote Removed in 14 and later
Safari on iOS – No support
Safari on iOS 5 – 6 (Release date: 2011-10-12)
footnote Removed in 7 and later
Safari on iOS – Full support
Safari on iOS 16 (Release date: 2022-09-12)
footnote Full support
Samsung Internet – No support
Samsung Internet 4 – 4.2 (Release date: 2016-03-11)
footnote Removed in 5 and later
WebView Android – Preview support
WebView Android 148 (Release date: 2026-05-05)
footnote Preview browser support
WebView on iOS – No support
WebView on iOS 5 – 6 (Release date: 2011-10-12)
footnote Removed in 7 and later
WebView on iOS – Full support
WebView on iOS 16 (Release date: 2022-09-12)
footnote Full support
Support for ECMAScript modules
Chrome – Full support
Chrome 80 (Release date: 2020-02-04)
footnote Full support
Edge – Full support
Edge 80 (Release date: 2020-02-07)
footnote Full support
Firefox – Full support
Firefox 114 (Release date: 2023-06-06)
footnote Full support
Opera – Full support
Opera 67 (Release date: 2020-03-03)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote
footnote Nested workers support was introduced in Safari 15.5.
footnote Script loading in nested workers was introduced in Safari 16.4.
Chrome Android – Preview support
Chrome Android 148 (Release date: 2026-05-05)
footnote Preview browser support
Firefox for Android – Full support
Firefox for Android 114 (Release date: 2023-06-06)
footnote Full support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote
footnote Nested workers support was introduced in Safari on iOS 15.5.
footnote Script loading in nested workers was introduced in Safari on iOS 16.4.
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Preview support
WebView Android 148 (Release date: 2026-05-05)
footnote Preview browser support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote
footnote Nested workers support was introduced in WebView on iOS 15.5.
footnote Script loading in nested workers was introduced in WebView on iOS 16.4.
url parameter requires TrustedScriptURL instance when trusted types are enforced
Chrome – Full support
Chrome 97 (Release date: 2022-01-04)
footnote Full support
Edge – Full support
Edge 97 (Release date: 2022-01-06)
footnote Full support
Firefox – Preview support
Firefox Nightly
footnote Preview browser support
Opera – Full support
Opera 83 (Release date: 2022-01-19)
footnote Full support
Safari – Full support
Safari 26 (Release date: 2025-09-15)
footnote Full support
Chrome Android – No support
Chrome Android
footnote No support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – Full support
Safari on iOS 26 (Release date: 2025-09-15)
footnote Full support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 26 (Release date: 2025-09-15)
footnote Full support
Strict MIME type checks for shared worker scripts
Chrome – No support
Chrome
footnote
footnote See bug 40554522
Edge – No support
Edge
footnote
footnote See bug 40554522
Firefox – Full support
Firefox 81 (Release date: 2020-09-22)
footnote Full support
Opera – No support
Opera
footnote
footnote See bug 40554522
Safari – Full support
Safari 16 (Release date: 2022-09-12)
footnote Full support
Chrome Android – No support
Chrome Android
footnote
footnote See bug 40554522
Firefox for Android – Full support
Firefox for Android 81 (Release date: 2020-09-22)
footnote Full support
Opera Android – No support
Opera Android
footnote
footnote See bug 40554522
Safari on iOS – Full support
Safari on iOS 16 (Release date: 2022-09-12)
footnote Full support
Samsung Internet – No support
Samsung Internet
footnote
footnote See bug 40554522
WebView Android – No support
WebView Android
footnote
footnote See bug 40554522
WebView on iOS – Full support
WebView on iOS 16 (Release date: 2022-09-12)
footnote Full support
options.extendedLifetime parameter
Experimental
Chrome – Preview support
Chrome 148 (Release date: 2026-05-05)
footnote Preview browser support
Edge – Preview support
Edge 148 (Release date: 2026-05-07)
footnote Preview browser support
Firefox – No support
Firefox
footnote No support
Opera – Preview support
Opera 132
footnote Preview browser support
Safari – No support
Safari
footnote No support
Chrome Android – Preview support
Chrome Android 148 (Release date: 2026-05-05)
footnote Preview browser support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Preview support
WebView Android 148 (Release date: 2026-05-05)
footnote Preview browser support
WebView on iOS – No support
WebView on iOS
footnote No support
options.name parameter
Chrome – Full support
Chrome 70 (Release date: 2018-10-16)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 55 (Release date: 2017-08-08)
footnote Full support
Opera – Full support
Opera 57 (Release date: 2018-11-28)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Preview support
Chrome Android 148 (Release date: 2026-05-05)
footnote Preview browser support
Firefox for Android – Full support
Firefox for Android 55 (Release date: 2017-08-08)
footnote Full support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Preview support
WebView Android 148 (Release date: 2026-05-05)
footnote Preview browser support
WebView on iOS – No support
WebView on iOS
footnote No support
options.sameSiteCookies parameter
Chrome – Full support
Chrome 125 (Release date: 2024-05-14)
footnote Full support
Edge – No support
Edge
footnote No support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 111 (Release date: 2024-06-12)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Preview support
Chrome Android 148 (Release date: 2026-05-05)
footnote Preview browser support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Preview support
WebView Android 148 (Release date: 2026-05-05)
footnote Preview browser support
WebView on iOS – No support
WebView on iOS
footnote No support
options.type parameter
Chrome – Full support
Chrome 80 (Release date: 2020-02-04)
footnote Full support
Edge – Full support
Edge 80 (Release date: 2020-02-07)
footnote Full support
Firefox – Full support
Firefox 114 (Release date: 2023-06-06)
footnote Full support
Opera – Full support
Opera 67 (Release date: 2020-03-03)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Preview support
Chrome Android 148 (Release date: 2026-05-05)
footnote Preview browser support
Firefox for Android – Full support
Firefox for Android 114 (Release date: 2023-06-06)
footnote Full support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Preview support
WebView Android 148 (Release date: 2026-05-05)
footnote Preview browser support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.
Has more compatibility info.

See also