aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/notification/onclick/index.md
blob: b338aa8813b224e6d9b35b9ebbb62c585fe85262 (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
---
title: Notification.onclick
slug: Web/API/notification/onclick
tags:
  - API
  - DOM
  - Notifications
  - Propriété
  - Reference
  - onclick
translation_of: Web/API/Notification/onclick
---
{{APIRef("Web Notifications")}}{{AvailableInWorkers}}{{securecontext_header}}

La propriété **`onclick`**, rattachée à l'interface {{domxref("Notification")}}, définit un gestionnaire d'évènement à déclencher lorsque la notification recçoit un évènement {{event("click")}} (qui se produit lorsqu'un utilisateur clique sur la notification).

## Syntaxe

    Notification.onclick = function(event) { ... };

Le comportement par défaut consiste à déplacer le focus sur la zone d'affichage (_viewport_) du [contexte de navigation](https://html.spec.whatwg.org/multipage/browsers.html#browsing-context) de la notification. Pour éviter ce comportement, on pourra appeler la méthode [`preventDefault()`](/fr/docs/Web/API/Event/preventDefault) sur l'objet représentant l'évènement.

## Exemples

Dans l'exemple qui suit, on utilise le gestionnaire d'évènement `onclick` pour ouvrir une page dans un nouvel onglet (avec le paramètre `'_blank'`) lorsqu'on clique sur la notification :

```js
notification.onclick = function(event) {
  event.preventDefault(); // empêcher le navigateur de passer le focus sur l'onglet de la navigation
  window.open('http://www.mozilla.org', '_blank');
}
```

## Spécifications

| Spécification                                                                                    | État                                     | Commentaires      |
| ------------------------------------------------------------------------------------------------ | ---------------------------------------- | ----------------- |
| {{SpecName('Web Notifications','#dom-notification-onclick','onclick')}} | {{Spec2('Web Notifications')}} | Standard évolutif |

## Compatibilité des navigateurs

{{Compat("api.Notification.onclick")}}

## Voir aussi

- {{domxref("Notification")}}
- [Utiliser l'API Notifications](/fr/docs/Web/API/notification/Using_Web_Notifications)