You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+17-2Lines changed: 17 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,6 +43,14 @@ $(document).on({
43
43
});
44
44
```
45
45
46
+
Or bind both to the same callback and distinguish using the event variable.
47
+
48
+
```js
49
+
$(document).on('show hide', function (e) {
50
+
console.log('The page is now', e.type==='show'?'visible':'hidden');
51
+
});
52
+
```
53
+
46
54
The plugin will detect if the Page Visibility API is natively supported in the browser or not, and expose this information as a boolean (`true`/`false`) in `$.support.pageVisibility`:
47
55
48
56
```js
@@ -51,9 +59,15 @@ if ($.support.pageVisibility) {
51
59
}
52
60
```
53
61
54
-
## Notes
62
+
If the Page Visibility API is supported the plugin will also store the current visibility state in `document.hidden`
55
63
56
-
Both events live under the `visibility` namespace — so if you ever need to remove all event handlers added by this plugin, you could just use `$(document).off('.visibility');` (or `$(document).unbind('.visibility');` in jQuery 1.6 or older).
64
+
```js
65
+
if (!document.hidden) {
66
+
// Page is currently visible
67
+
}
68
+
```
69
+
70
+
## Notes
57
71
58
72
This plugin is not a Page Visibility [polyfill](http://mths.be/polyfills), as it doesn’t aim to mimic the standard API. It merely provides a simple way to use this functionality (or a fallback) in your jQuery code.
59
73
@@ -68,3 +82,4 @@ This plugin is available under the MIT license.
0 commit comments