aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/workerglobalscope/importscripts/index.html
blob: 9a7781a1b81c9d690a7064214da4ef3c6658215a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
title: WorkerGlobalScope.importScripts()
slug: Web/API/WorkerGlobalScope/importScripts
tags:
  - API
  - Web Workers
  - WorkerGlobalScope
  - Workers
  - 参考
  - 方法
translation_of: Web/API/WorkerGlobalScope/importScripts
---
<p>{{APIRef("Web Workers API")}}</p>

<p>{{domxref("WorkerGlobalScope")}} 接口的<code><strong>importScripts()</strong></code> 方法将一个或多个脚本同步导入到工作者的作用域中。</p>

<h2 id="语法">语法</h2>

<pre class="brush: js">self.importScripts('foo.js');
self.importScripts('foo.js', 'bar.js', ...);</pre>

<h3 id="参数">参数</h3>

<p>{{domxref("DOMString")}} 对象的一个逗号分隔列表,表示要导入的脚本。</p>

<h3 id="返回值">返回值</h3>

<p><em>无。</em></p>

<h3 id="异常">异常</h3>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">异常</th>
   <th scope="col">描述</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>NetworkError</code></td>
   <td>要导入的脚本不具有有效的 JavaScript MIME 类型(有效的类型如 <code>text/javascript</code>)。</td>
  </tr>
 </tbody>
</table>

<h2 id="例子">例子</h2>

<p>如果您在一个名为foo.js的单独脚本中编写了一些您想在worker.js中使用的功能,则可以使用以下行导入它:</p>

<pre class="brush: js">importScripts('foo.js');</pre>

<p><code>importScripts()</code> 和 <code>self.importScripts()</code> 实际上是等效的 — 都表示从工作者的内部范围内调用的 <code>importScripts()</code></p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', '#dom-workerglobalscope-importscripts', 'importScripts()')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.WorkerGlobalScope.importScripts")}}</p>

<h2 id="也可以看看">也可以看看</h2>

<ul>
 <li>{{domxref("WorkerGlobalScope")}}</li>
 <li><a href="/zh-CN/docs/Web/API/Web_Workers_API/Using_web_workers">Using Web workers</a></li>
</ul>