aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/window/unload_event/index.html
blob: 1803dc1f988325762dc5395c18f622179728fbee (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
title: unload
slug: Web/API/Window/unload_event
tags:
  - JavaScript
  - events
translation_of: Web/API/Window/unload_event
original_slug: Web/Events/unload
---
<p><br>
 <span class="seoSummary">L'événement <code>unload</code> est appelé lorsque le document ou une ressource enfant est en train d'être déchargé.</span></p>

<p>Il est lancé après :</p>

<ol>
 <li><a href="/en-US/docs/Mozilla_event_reference/beforeunload" title="/en-US/docs/Mozilla_event_reference/beforeunload">beforeunload</a> (événement annulable)</li>
 <li><a href="/en-US/docs/Mozilla_event_reference/pagehide" title="/en-US/docs/Mozilla_event_reference/pagehide">pagehide</a></li>
</ol>

<p>Le document se trouve alors dans un état particulier :</p>

<ul>
 <li>Toutes les ressources existent encore (img, iframe etc.)</li>
 <li>Plus rien n'est encore visible par l'utilisateur final</li>
 <li>Les intéractions avec l'interface sont désactivées (<code>window.open</code>, <code>alert</code>, <code>confirm</code>, etc.)</li>
 <li>Aucune erreur ne viendra interrompre le flux de déchargement.</li>
</ul>

<p>Veuiller noter que l'événement <code>unload</code> suit l'ordre du document : le cadre parent est déchargé <em>avant</em> le <code>unload</code> d'un cadre enfant (voir l'exemple ci-dessous).</p>

<table class="properties">
 <tbody>
  <tr>
   <td>Événement propageable</td>
   <td>Non</td>
  </tr>
  <tr>
   <td>Annulable</td>
   <td>Non</td>
  </tr>
  <tr>
   <td>Objets cibles</td>
   <td>defaultView, Document, Element</td>
  </tr>
  <tr>
   <td>Interface</td>
   <td>{{domxref("UIEvent")}} si généré depuis un élément de l'interface utilisateur, {{domxref("Event")}}</td>
  </tr>
  <tr>
   <td>Action par défaut</td>
   <td>Aucune</td>
  </tr>
 </tbody>
</table>

<h2 id="Propriétés">Propriétés</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Propriété</th>
   <th scope="col">Type</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>target</code> {{readonlyInline}}</td>
   <td><a href="/en-US/docs/Web/API/EventTarget" title="EventTarget is an interface implemented by objects that can receive events and may have listeners for them."><code>EventTarget</code></a></td>
   <td>La cible de l'événement (la cible de plus haut niveau dans le DOM).</td>
  </tr>
  <tr>
   <td><code>type</code> {{readonlyInline}}</td>
   <td><a href="/en-US/docs/Web/API/DOMString" title="DOMString is a UTF-16 String. As JavaScript already uses such strings, DOMString is mapped directly to a String."><code>DOMString</code></a></td>
   <td>Le type d'événement.</td>
  </tr>
  <tr>
   <td><code>bubbles</code> {{readonlyInline}}</td>
   <td><a href="/en-US/docs/Web/API/Boolean" title="The Boolean object is an object wrapper for a boolean value."><code>Boolean</code></a></td>
   <td>Si l'événement remonte ou non.</td>
  </tr>
  <tr>
   <td><code>cancelable</code> {{readonlyInline}}</td>
   <td><a href="/en-US/docs/Web/API/Boolean" title="The Boolean object is an object wrapper for a boolean value."><code>Boolean</code></a></td>
   <td>Si l'événement est annulable ou non.</td>
  </tr>
  <tr>
   <td><code>view</code> {{readonlyInline}}</td>
   <td><a class="new" href="/en-US/docs/Web/API/WindowProxy" rel="nofollow" title="The documentation about this has not yet been written; please consider contributing!"><code>WindowProxy</code></a></td>
   <td><a href="/en-US/docs/Web/API/Document/defaultView" title="In browsers, document.defaultView returns the window object associated with a document, or null if none is available."><code>document.defaultView</code></a> (<code>fenêtre</code> du document)</td>
  </tr>
  <tr>
   <td><code>detail</code> {{readonlyInline}}</td>
   <td><code>long</code> (<code>float</code>)</td>
   <td>0.</td>
  </tr>
 </tbody>
</table>

<h2 id="Exemple">Exemple</h2>

<pre class="brush: html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Cadre parent&lt;/title&gt;
    &lt;script&gt;
      window.addEventListener('beforeunload', function(event) {
        console.log('Je suis le 1er.');
      });
      window.addEventListener('unload', function(event) {
        console.log('Je suis le 3ème.');
      });
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;iframe src="child-frame.html"&gt;&lt;/iframe&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>

<p>Ci-dessous, le contenu de <code>child-frame.html</code>:</p>

<pre class="brush: html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Cadre enfant&lt;/title&gt;
    &lt;script&gt;
      window.addEventListener('beforeunload', function(event) {
        console.log('Je suis le 2nd.');
      });
      window.addEventListener('unload', function(event) {
        console.log('Je suis le 4ème et dernier…');
      });
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      ☻
  &lt;/body&gt;
&lt;/html&gt;</pre>

<p>Quand le cadre parent est déchargé, les événements sont lancés dans l'ordre décrit par les messages <code>console.log</code>.</p>

<h2 id="Événements_liés">Événements liés</h2>

<ul>
 <li>{{event("DOMContentLoaded")}}</li>
 <li>{{event("readystatechange")}}</li>
 <li>{{event("load")}}</li>
 <li>{{event("beforeunload")}}</li>
 <li>{{event("unload")}}</li>
</ul>

<h2 id="Spécifications">Spécifications</h2>

<ul>
 <li><a href="https://html.spec.whatwg.org/multipage/browsers.html#unloading-documents" title="http://www.whatwg.org/specs/web-apps/current-work/#unload-a-document">Unloading Documents — unload a document</a></li>
 <li><a href="http://www.w3.org/TR/DOM-Level-3-Events/#event-type-unload" title="http://www.w3.org/TR/DOM-Level-3-Events/#event-type-unload">Event Module Definition — unload</a></li>
</ul>