blob: 18a85d75383bfc3f8d1c9692bd79eb733150a01e (
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
|
---
title: Clients.openWindow()
slug: Web/API/Clients/openWindow
translation_of: Web/API/Clients/openWindow
---
<p>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</p>
<p>{{domxref("Clients")}}接口的 <strong><code>openWindow()</code></strong> 方法创建一个顶级的浏览器上下文并加载给定的 URL. 如果调用脚本没有显示弹出窗口的权限, <strong><code>openWindow() </code></strong>将抛出 InvalidAccessError.</p>
<p>在Firefox中,只有在作为通知点击事件的结果调用时,才允许该方法显示弹出窗口.</p>
<p>在Chrome for Android中,该方法可以改为在先前添加到用户主屏幕的 <a href="https://developer.mozilla.org/en-US/Apps/Progressive/Installable">standalone web app</a> 提供的现有浏览上下文中打开URL.</p>
<h2 id="语法">语法</h2>
<pre class="brush: js">ServiceWorkerClients.openWindow(url).then(function(WindowClient) {
// do something with your WindowClient
});</pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>url</code></dt>
<dd>一个 {{domxref("USVString")}} ,表示要在窗口中打开的client的URL。 通常,此值必须是与调用脚本有相同域的URL.</dd>
</dl>
<h3 id="返回值">返回值</h3>
<dl>
<dd>如果URL来自与服务工作者相同的域,则resolve为 {{domxref("WindowClient")}} 对象的Promise,否则resolve为 {{Glossary("null", "null value")}} .</dd>
</dl>
<h2 id="示例">示例</h2>
<pre class="brush: js">// When the user clicks a notification focus the window if it exists or open
// a new one otherwise.
onotificationclick = function(event) {
var found = false;
clients.matchAll().then(function(clientsArr) {
for (i = 0; i < clientsArr.length; i++) {
if (clientsArr[i].url === event.data.url) {
// We already have a window to use, focus it.
found = true;
clientsArr[i].focus();
break;
}
}
if (!found) {
// Create a new window.
clients.openWindow(event.data.url).then(function(windowClient) {
// do something with the windowClient.
});
}
});
};
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('Service Workers', '#clients', 'Clients')}}</td>
<td>{{Spec2('Service Workers')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("api.Clients.openWindow")}}</p>
</div>
|