This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.html
435 lines (431 loc) · 15 KB
/
index.html
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>
Page Visibility
</title>
<script src="https://www.w3.org/Tools/respec/respec-w3c" class=
"remove"></script>
<script class='remove'>
var respecConfig = {
level: "2",
specStatus: "DISC",
editors: [
{
name: "Ilya Grigorik",
url: "https://www.igvita.com/",
company: "Shopify Inc.",
companyURL: "https://shopify.com/",
w3cid: "56102",
},
{
name: "Marcos Cáceres",
url: "https://marcosc.com/",
company: "W3C",
companyURL: "https://w3.org/",
w3cid: 39125,
},
{
name: "Arvind Jain",
company: "Google Inc.",
companyURL: "https://google.com/",
retiredDate: "2014-12-01",
},
{
name: "Jatinder Mann",
company: "Microsoft Corp.",
companyURL: "https://microsoft.com",
retiredDate: "2014-02-01",
},
],
github: "w3c/page-visibility",
group: "webperf",
implementationReportURI: "https://wpt.fyi/page-visibility/",
testSuiteURI: "https://wpt.live/page-visibility/",
xref: "web-platform",
};
</script>
</head>
<body>
<section id="abstract">
<p>
This specification defines a means to programmatically determine the
visibility state of a document. This can aid in the development of
resource efficient web applications.
</p>
</section>
<section id="sotd">
<p style=
'border: 5px solid red; border-radius: 10px; padding: 1em; margin: 1em; text-align: center; font-weight: 300; font-size: 120%;'>
🚩<br>
This document is retired and <strong>MUST NOT</strong> be used for
further technical work.<br>
See the <a href=
"https://html.spec.whatwg.org/multipage/interaction.html#page-visibility">
HTML Living Standard</a> Page Visibility section instead and raise
issues in the <a href=
"https://github.com/whatwg/html/issues">whatwg/html repository</a>.
</p>
</section>
<section id="introduction" class='informative'>
<h2>
Introduction
</h2>
<p>
The <cite>Page Visibility API</cite> defines a means to
programmatically determine the [=Document/visibility state=] of a
top-level browsing context, and to be notified if the
[=Document/visibility state=] changes. Without knowing the
[=Document/visibility state=] of a page, web developers have been
designing web pages as if they are always [=Document/visible=]. This
not only results in higher machine resource utilization, but it
prevents web developers from making runtime decisions based on whether
the web page is [=Document/visible=] to the user. Designing web pages
with knowledge of the document's [=Document/visibility state=] can
result in improved user experiences and power efficient sites.
</p>
<p>
With this API, web applications can choose to alter their behavior
based on whether they are [=Document/visible=] to the user or not. For
example, this API can be used to scale back work when the page is no
longer [=Document/visible=].
</p>
</section>
<section class='informative'>
<h2>
Examples of usage
</h2>
<p>
To improve the user experience and optimize CPU and power efficiency
the application could autoplay a video when the application is
[=Document/visible=], and automatically pause the playback when the
application is [=Document/hidden=]:
</p>
<pre class="example js" title="Visibility-aware video playback">
const videoElement = document.getElementById("videoElement");
// Autoplay the video if application is visible
if (document.visibilityState === "visible") {
videoElement.play();
}
// Handle page visibility change events
function visibilityListener() {
switch(document.visibilityState) {
case "hidden":
videoElement.pause();
break;
case "visible":
videoElement.play();
break;
}
}
document.addEventListener("visibilitychange", visibilityListener);
</pre>
<p>
Similar logic can be applied to intelligently pause and resume, or
throttle, execution of application code such as animation loops,
analytics, and other types of processing. By combining the
{{Document/visibilityState}} attribute of the {{Document}} interface
and the `"visibilitychange"` event, the application is able to both
query and listen to page visibility events to deliver a better user
experience, as well as improve efficiency and performance of its
execution.
</p>
</section>
<section>
<h2>
Visibility states
</h2>
<p>
The [=document=] of the <a>top-level browsing context</a> can be in one
of the following <dfn data-dfn-for="Document" data-export="">visibility
states</dfn>:
</p>
<dl data-sort="">
<dt>
<dfn data-dfn-for="Document" data-export="" data-dfn-type=
"dfn">hidden</dfn>
</dt>
<dd>
<p>
The [=document=] is not [=Document/visible=] at all on any screen,
as determined by running the steps to [=determine the visibility
state=] of the [=document=]'s [=relevant global object=]'s
[=browsing context=].
</p>
<aside class="note">
<p>
Examples of actions that can cause a document to be
[=Document/hidden=]:
</p>
<ul>
<li>A tab is made into a background tab.
</li>
<li>The user agent is minimized.
</li>
<li>The user agent is moved off of the screen.
</li>
<li>The operating system's lock screen covers the user agent.
</li>
</ul>
</aside>
</dd>
<dt>
<dfn data-dfn-for="Document" data-export="" data-dfn-type=
"dfn">visible</dfn>
</dt>
<dd>
The [=document=] is at least partially visible on at least one
screen, as determined by running the steps to [=determine the
visibility state=] of the [=document=]'s [=relevant global object=]'s
[=browsing context=].
</dd>
</dl>
<p>
The [=Document/visibility states=] are reflected in the API via the
{{VisibilityState}} enum.
</p>
<p>
The steps to <dfn data-export="" data-dfn-type="dfn">determine the
visibility state</dfn> of a [=browsing context=] |context| are as
follows. The steps return a [=Document/visibility state=].
</p>
<ol class="algorithm">
<li>Let |doc:Document| be the |context|'s [=top-level browsing
context=]'s [=active document=].
</li>
<li>Assert: |doc| is a [=Document=].
</li>
<li>Return [=Document/visible=] if:
<ul>
<li>The user agent has assistive technologies attached to the
|doc|.
</li>
<li>Any of the |doc|'s viewport contents are visible to the user.
</li>
</ul>
</li>
<li>Otherwise, return [=Document/hidden=].
</li>
</ol>
</section>
<section data-dfn-for="VisibilityState">
<h2>
<code>VisibilityState</code> enum
</h2>
<pre class="idl">
enum VisibilityState {
"hidden", "visible"
};
</pre>
<p>
The <dfn>VisibilityState</dfn> enum is used to represent the
[=Document/visibility states=].
</p>
<p>
The "<dfn>hidden</dfn>" enum value represents the [=Document/hidden=]
[=Document/visibility state=].
</p>
<p>
Likewise, the "<dfn>visible</dfn>" enum value represents the
[=Document/visible=] [=Document/visibility state=].
</p>
</section>
<section data-dfn-for="Document">
<h2>
Extensions to the `Document` interface
</h2>
<p>
This specification extends the {{Document}} interface:
</p>
<pre class="idl">
partial interface Document {
readonly attribute boolean hidden;
readonly attribute VisibilityState visibilityState;
attribute EventHandler onvisibilitychange;
};
</pre>
<section>
<h3>
<dfn>hidden</dfn> attribute
</h3>
<p>
On getting, the {{Document/hidden}} attribute MUST:
</p>
<ol class="algorithm">
<li>Let |state:Visibility State| be the result of [=determine the
visibility state=] of [=this=]'s [=relevant global object=]'s
[=browsing context=].
</li>
<li>If |state| is [=Document/visible=], then return false. Return
true otherwise.
</li>
</ol>
</section>
<section>
<h3>
<dfn>visibilityState</dfn> attribute
</h3>
<p>
On getting, the {{Document/visibilityState}} attribute the user agent
MUST:
</p>
<ol class="algorithm">
<li>Let |state:Visibility State| be the result of [=determine the
visibility state=] of [=this=]'s [=relevant global object=]'s
[=browsing context=].
</li>
<li>If |state| is [=Document/visible=], return
{{VisibilityState/"visible"}}. Otherwise, return
{{VisibilityState/"hidden"}}.
</li>
</ol>
</section>
<section>
<h3>
<dfn>onvisibilitychange</dfn> event handler attribute
</h3>
<p>
The {{Document/onvisibilitychange}} attribute is an <a>event handler
IDL attribute</a> for the `"visibilitychange"` event type (see
[[[#reacting-to-visibilitychange]]]).
</p>
</section>
</section>
<section>
<h2 id="reacting-to-visibilitychange">
Reacting to `"visibilitychange"` changes
</h2>
<p>
The <a>task source</a> for these <a>tasks</a> is the <a>user
interaction task source</a>.
</p>
<p>
When the user agent determines that the visibility of the {{Document}}
of the <a>top-level browsing context</a> has changed, the user agent
MUST run the following steps:
</p>
<ol class="algorithm">
<li>Let |doc:Document| be the {{Document}} of the <a>top-level browsing
context</a>.
</li>
<li>If |doc| is now [=Document/visible=]:
<ol>
<li>If traversing to a <a>session history entry</a>, run the <a>now
visible algorithm</a> before running the step to fire the
[=Window/pageshow=] event.
</li>
<li>Otherwise, <a>queue a task</a> that runs the <a>now visible
algorithm</a>.
</li>
</ol>
</li>
<li>Else if |doc| is now not [=Document/visible=], or if the user agent
is to [=Window/unload=] |doc|:
<ol>
<li>If the user agent is to [=Window/unload=] the {{Document}}, run
the <a>now hidden algorithm</a> during the <a>unloading document
visibility change steps</a>.
</li>
<li>Otherwise, <a>queue a task</a> that runs the <a>now hidden
algorithm</a>.
</li>
</ol>
</li>
</ol>
<p>
The <dfn data-export="" data-dfn-type="abstract-op">now visible
algorithm</dfn> runs the following steps synchronously:
</p>
<ol class="algorithm">
<li>Let |doc:Document| be the {{Document}} of the <a>top-level browsing
context</a>.
</li>
<li>
<a>Fire an event</a> named "`visibilitychange`" that bubbles, isn't
cancelable, and has no default action, at the |doc|.
</li>
<li>Run <dfn data-export="" data-dfn-type="abstract-op">external now
visible algorithm</dfn> if one is defined by another specification.
</li>
</ol>
<p>
The <dfn data-export="" data-dfn-type="abstract-op">now hidden
algorithm</dfn> runs the following steps synchronously:
</p>
<ol class="algorithm">
<li>Let |doc:Document| be the {{Document}} of the <a>top-level browsing
context</a>.
</li>
<li>
<a>Fire an event</a> named "`visibilitychange`" that bubbles, isn't
cancelable, and has no default action, at the |doc|.
</li>
<li>Run <dfn data-export="">external now hidden algorithm</dfn> if one
is defined by another specification.
</li>
</ol>
</section>
<section id="privacy-security">
<h2>
Privacy and Security
</h2>
<p>
The <cite>Page Visibility API</cite> enables developers to know when a
{{Document}} is visible or in focus. Existing mechanisms, such as the
[=Window/focus=] and [=Window/blur=] events, when attached to the
{{Window}} object already provide a mechanism to detect when the
{{Document}} is the active document; the [=Window/unload=] event
provides a notification that the page is being unloaded.
</p>
</section>
<section>
<h2>
Accessibility considerations
</h2>
<p>
To accommodate assistive technologies that are typically full screen
but still show a view of the page, when applicable, on getting, the
{{Document/visibilityState}} attribute MAY return
{{VisibilityState/"visible"}}, instead of {{VisibilityState/"hidden"}},
when the user agent is not minimized but is fully obscured by other
applications.
</p>
</section>
<section>
<h3>
Terminology
</h3>
<p>
The following concepts and interfaces are defined in the [[!HTML]]
specification:
</p>
<ul data-sort="">
<li>
<dfn data-cite="!HTML/browsers.html#session-history-entry">session
history entry</dfn>
</li>
<li>
<dfn data-cite=
"!HTML/browsers.html#unloading-document-visibility-change-steps">unloading
document visibility change steps</dfn>
</li>
</ul>
</section>
<section id="conformance">
<!-- autofilled by respec -->
</section>
<section id="index" class="appendix"></section>
<section id="acknowledgements" class="appendix">
<h2>
Acknowledgments
</h2>
<p>
Thanks to Alex Komoroske, Arvind Jain, Boris Zbarsky, Cameron
McCormack, James Robinson, Jason Weber, Jonas Sicking, Karen Anderson,
Kyle Simpson, Nat Duca, Nic Jansma, Philippe Le Hegaret, and Todd
Reifsteck for their contributions to this work.
</p>
</section>
</body>
</html>