Worker() constructor creates a Worker object that executes the script at the specified URL. This script must obey the same-origin policy .

注意 : that there is a disagreement among browser manufacturers about whether a data URI is of the same origin or not. Though Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7) and later accept data URIs, that's not the case in all other browsers.

句法

var myWorker = new Worker(aURL, options);
					

参数

aURL
USVString representing the URL of the script the worker will execute. It must obey the same-origin policy.
选项 可选
An object containing option properties that can be set when creating the object instance. Available properties are as follows:
  • type : A DOMString specifying the type of worker to create. The value can be classic or 模块 . If not specified, the default used is classic .
  • credentials : A DOMString specifying the type of credentials to use for the worker. The value can be omit , same-origin ,或 包括 . If not specified, or if type is classic , the default used is omit (no credentials required).
  • 名称 : A DOMString specifying an identifying name for the DedicatedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.

异常

  • A SecurityError is raised 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.
  • A NetworkError is raised if the MIME type of the worker script is incorrect. It should always be text/javascript .
  • A SyntaxError 被引发若 aUR L 无法剖析。

范例

The following code snippet shows creation of a Worker 对象使用 Worker() constructor and subsequent usage of the object:

var myWorker = new Worker('worker.js');
first.onchange = function() {
  myWorker.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}
					

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

规范

规范 状态 注释
HTML 实时标准
The definition of 'Worker()' in that specification.
实时标准

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Worker() 构造函数 Chrome 4 Edge 12 Firefox 3.5 IE 10 Opera 10.6 Safari 4 WebView Android 4 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 5.1 Samsung Internet Android 1.0
Support for ECMAScript modules Chrome 80 Edge 80 Firefox No IE No Opera 67 Safari No WebView Android 80 Chrome Android 80 Firefox Android No Opera Android 57 Safari iOS No Samsung Internet Android No
Strict MIME type checks for worker scripts Chrome ? Edge ? Firefox 70 IE No Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android No Opera Android ? Safari iOS ? Samsung Internet Android ?
构造函数 名称 option Chrome 70 Edge 18 Firefox 55 IE No Opera 57 Safari No
不支持 No
Supported in Safari Technology Preview 64
WebView Android No Chrome Android 70 Firefox Android 55 Opera Android 49 Safari iOS No
不支持 No
Supported in Safari Technology Preview 64
Samsung Internet Android 10.0
构造函数 type option Chrome 80 Edge 80 Firefox No IE No Opera 67 Safari No WebView Android 80 Chrome Android 80 Firefox Android No Opera Android 57 Safari iOS No Samsung Internet Android No

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

见实现注意事项。

注意 : A browser can be marked as providing full support for Worker() even though it does not support worker scripts written as modules. As of Mar 1, 2019, only Chrome 80+ supports this feature, while Firefox has an open feature request .  No other browsers are known to have support for production usage of worker scripts written as modules. Without that support, worker scripts written as modules and modules used by worker scripts have to be transpiled or otherwise converted to non-module code in order to run.

另请参阅

Worker interface it belongs to.

元数据

  • 最后修改: