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

注意 : there is disagreement among browser manufacturers about whether a data URI is of the same origin or not. Although 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 SharedWorker(aURL, name);
var myWorker = new SharedWorker(aURL, options);
					

参数

aURL
DOMString representing the URL of the script the worker will execute. It must obey the same-origin policy.
名称 可选
DOMString specifying an identifying name for the SharedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.
选项 可选
An object containing option properties that can 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 SharedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes.

返回值

The created worker.

异常

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

范例

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

var myWorker = new SharedWorker('worker.js');
myWorker.port.start();
first.onchange = function() {
  myWorker.port.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}
second.onchange = function() {
  myWorker.port.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}
myWorker.port.onmessage = function(e) {
  result1.textContent = e.data;
  console.log('Message received from worker');
}
					

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

规范

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

浏览器兼容性

The compatibility table on 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
SharedWorker() 构造函数 Chrome 4 Edge 79 Firefox 29 IE No Opera 10.6 Safari 5 — 6.1 WebView Android No Chrome Android No Firefox Android 33 Opera Android 11 — 14 Safari iOS 5.1 — 7 Samsung Internet Android 4.0 — 5.0
Strict MIME type checks for shared worker scripts Chrome ? Edge ? Firefox 70 IE No Opera ? Safari No WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
名称 option Chrome Yes Edge ≤79 Firefox 55 IE No Opera ? Safari No WebView Android No Chrome Android No Firefox Android 55 Opera Android No Safari iOS No Samsung Internet Android 4.0 — 5.0

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: