From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/clients/openwindow/index.html | 81 +++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 files/zh-cn/web/api/clients/openwindow/index.html (limited to 'files/zh-cn/web/api/clients/openwindow') diff --git a/files/zh-cn/web/api/clients/openwindow/index.html b/files/zh-cn/web/api/clients/openwindow/index.html new file mode 100644 index 0000000000..18a85d7538 --- /dev/null +++ b/files/zh-cn/web/api/clients/openwindow/index.html @@ -0,0 +1,81 @@ +--- +title: Clients.openWindow() +slug: Web/API/Clients/openWindow +translation_of: Web/API/Clients/openWindow +--- +

{{SeeCompatTable}}{{APIRef("Service Workers API")}}

+ +

{{domxref("Clients")}}接口的 openWindow() 方法创建一个顶级的浏览器上下文并加载给定的 URL. 如果调用脚本没有显示弹出窗口的权限, openWindow() 将抛出 InvalidAccessError.

+ +

在Firefox中,只有在作为通知点击事件的结果调用时,才允许该方法显示弹出窗口.

+ +

在Chrome for Android中,该方法可以改为在先前添加到用户主屏幕的 standalone web app 提供的现有浏览上下文中打开URL.

+ +

语法

+ +
ServiceWorkerClients.openWindow(url).then(function(WindowClient) {
+  // do something with your WindowClient
+});
+ +

参数

+ +
+
url
+
一个 {{domxref("USVString")}} ,表示要在窗口中打开的client的URL。 通常,此值必须是与调用脚本有相同域的URL.
+
+ +

返回值

+ +
+
如果URL来自与服务工作者相同的域,则resolve为 {{domxref("WindowClient")}} 对象的Promise,否则resolve为 {{Glossary("null", "null value")}} .
+
+ +

示例

+ +
// 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.
+      });
+    }
+  });
+};
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#clients', 'Clients')}}{{Spec2('Service Workers')}}Initial definition.
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.Clients.openWindow")}}

+
-- cgit v1.2.3-54-g00ecf