Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Worker
  4. Worker()

Worker: Worker() constructor

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 available inWeb Workers, except forService Workers.

TheWorker() constructor creates aWorker object that executes the script at the specified URL. This script must obey thesame-origin policy.

Note:There is a disagreement among browser manufacturers about whether a data URL is of the same origin or not. Though Firefox 10 and later accept data URLs, that's not the case in all other browsers.

Syntax

js
new Worker(url)new Worker(url, options)

Parameters

url

A string representing the URL of the script the worker will execute. It must obey the same-origin policy. The URL is resolved relative to the current HTML page's location.

Note:Bundlers, includingwebpack,Vite, andParcel, recommend passing URLs that are relative toimport.meta.url to theWorker() constructor. For example:

js
const myWorker = new Worker(new URL("worker.js", import.meta.url));

This way, the path is relative to the current script instead of the current HTML page, which allows the bundler to safely do optimizations like renaming (because otherwise theworker.js URL may point to a file not controlled by the bundler, so it cannot make any assumptions).

optionsOptional

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

type

A string specifying the type of worker to create. The value can beclassic ormodule. If not specified, the default used isclassic.

credentials

A string specifying the type of credentials to use for the worker. The value can beomit,same-origin, orinclude. If not specified, or if type isclassic, the default used issame-origin (only include credentials for same-origin requests).

name

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

Exceptions

SecurityErrorDOMException

Thrown if the document is not allowed to start workers, e.g., if the URL has an invalid syntax or if the same-origin policy is violated.

NetworkErrorDOMException

Thrown if the MIME type of the worker script is incorrect. Itshould always betext/javascript(for historical reasonsother JavaScript MIME types may be accepted).

SyntaxErrorDOMException

Thrown ifaURL cannot be parsed.

Examples

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

js
const myWorker = new Worker("worker.js");const first = document.querySelector("input#number1");first.onchange = () => {  myWorker.postMessage(first.value);  console.log("Message posted to worker");};

For a full example, see ourBasic dedicated worker example (run dedicated worker).

Specifications

Specification
HTML
# dom-worker-dev

Browser compatibility

See also

TheWorker interface it belongs to.

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp