Worker() - Web APIs 编辑

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

Note: 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.

Syntax

var myWorker = new Worker(aURL, options);

Parameters

aURL
A USVString representing the URL of the script the worker will execute. It must obey the same-origin policy.
options Optional
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 module. If not specified, the default used is classic.
  • credentials: A DOMString specifying the type of credentials to use for the worker. The value can beomit, same-origin, or include. If not specified, or if type is classic, the default used is omit (no credentials required).
  • name: ADOMString specifying an identifying name for the DedicatedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.

Exceptions

  • 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 (for historical reasons other JavaScript MIME types may be accepted).
  • A SyntaxError is raised if aURL cannot be parsed.

Examples

The following code snippet shows creation of a Worker object using the 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).

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'Worker()' in that specification.
Living Standard

Browser compatibility

BCD tables only load in the browser

Note: 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.

See also

The Worker interface it belongs to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:148 次

字数:5362

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文