This feature is not Baseline because it does not work in some of the most widely-used browsers.
The SharedWorker() constructor creates a SharedWorker object that executes the script at the specified URL. This script must obey the same-origin policy.
Note: There is disagreement among browser manufacturers about whether a data URL is of the same origin or not. Although Firefox 10.0 and later accept data URLs, that's not the case in all other browsers.
new SharedWorker(url) new SharedWorker(url, name) new SharedWorker(url, options)
urlA string representing the URL of the script the worker will execute. It must obey the same-origin policy.
name OptionalA 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 OptionalAn object containing option properties that can set when creating the object instance. Available properties are as follows:
typeA string specifying the type of worker to create. The value can be classic or module. If not specified, the default used is classic.
credentialsA string specifying the type of credentials to use for the worker. The value can be omit, same-origin, or include. If not specified, or if type is classic, the default used is omit (no credentials required).
nameA string specifying an identifying name for the SharedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.
A string indicating which SameSite cookies should be available to the worker. Can have one of the following two values:
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.
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.
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.
The following code snippet shows creation of a SharedWorker object using the SharedWorker() constructor and subsequent usage of the object:
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.)
| Specification |
|---|
| HTML> # dom-sharedworker-dev> |
| 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 |
5 | 79 | 29 | 10.6 | 165–7 | No | 33 | 11–14 | 165–7 | 4.0–5.0 | No | 165–7 |
ecmascript_modules |
80 | 80 | 114 | 67 | 15["Nested workers support was introduced in Safari 15.5.", "Script loading in nested workers was introduced in Safari 16.4."] |
No | 114 | No | 15["Nested workers support was introduced in Safari on iOS 15.5.", "Script loading in nested workers was introduced in Safari on iOS 16.4."] |
No | No | 15["Nested workers support was introduced in WebView on iOS 15.5.", "Script loading in nested workers was introduced in WebView on iOS 16.4."] |
mime_checks |
No | No | 81 | No | 16 | No | 81 | No | 16 | No | No | 16 |
options_name_parameter |
70 | 79 | 55 | 57 | No | No | 55 | No | No | No | No | No |
options_sameSiteCookies_parameter |
125 | No | No | 111 | No | No | No | No | No | No | No | No |
options_type_parameter |
80 | 80 | 114 | 67 | 15 | No | 114 | No | 15 | No | No | 15 |
SharedWorker interface it belongs to.
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/SharedWorker