aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/windowclient/index.md
blob: 3fbcd1fde0ad6a55bb44e24904bafffe4ee9d058 (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
---
title: WindowClient
slug: Web/API/WindowClient
tags:
  - API
  - Client
  - Experimental
  - Interface
  - Reference
  - Service Workers
  - ServiceWorker
  - WindowClient
translation_of: Web/API/WindowClient
---
{{SeeCompatTable}}{{APIRef("Service Workers API")}}

L'interface `WindowClient` de l'[API ServiceWorker](/en-US/docs/Web/API/ServiceWorker_API) représente la portée d'un client service worker lorsque celui-ci existe en tant que document dans un contexte navigateur, controlé par un worker actif. Le client sélectionne et utilise un service worker pour son propre chargement et celui de ses sous-ressources.

## Méthodes

_`WindowClient` hérite certaines méthodes de son parent, {{domxref("Client")}}._

- {{domxref("WindowClient.focus()")}}
  - : Assigne le focus au client en cours.
- {{domxref("WindowClient.navigate()")}}
  - : Charge l'url spécifiée dans la page en cours.

## Propriétés

_`WindowClient` hérite certaines propriétés de son parent, {{domxref("Client")}}._

- {{domxref("WindowClient.focused")}} {{readonlyInline}}
  - : Un booléen qui indique si oui ou non le client en cours est focus.
- {{domxref("WindowClient.visibilityState")}} {{readonlyInline}}
  - : Indique la visibilité du client en cours. Peut prendre les valeures `hidden`, `visible`, `prerender`, ou `unloaded`.

## Exemple

```js
self.addEventListener('notificationclick', function(event) {
  console.log('On notification click: ', event.notification.tag);
  event.notification.close();

  // Vérifie si le client en cours est ouvert et
  // le focus le cas échéant
  event.waitUntil(clients.matchAll({
    type: "window"
  }).then(function(clientList) {
    for (var i = 0; i < clientList.length; i++) {
      var client = clientList[i];
      if (client.url == '/' && 'focus' in client)
        return client.focus();
    }
    if (clients.openWindow)
      return clients.openWindow('/');
  }));
});
```

## Spécifications

| Spécification                                                                                        | État                                 | Commentaires         |
| ---------------------------------------------------------------------------------------------------- | ------------------------------------ | -------------------- |
| {{SpecName('Service Workers', '#window-client-interface', 'WindowClient')}} | {{Spec2('Service Workers')}} | Définition initiale. |

## Compatibilité des navigateurs

{{Compat("api.WindowClient")}}

## Voir aussi

- [Using Service Workers](/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers)
- [Service workers basic code example](https://github.com/mdn/sw-test)
- [Is ServiceWorker ready?](https://jakearchibald.github.io/isserviceworkerready/)
- [Promises](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
- [Using web workers](/en-US/docs/Web/Guide/Performance/Using_web_workers)
- [Channel Messaging API](/en-US/docs/Web/API/Channel_Messaging_API)