Skip to content

Commit b8eb6fc

Browse files
committed
Merge pull request #19 from janpaepke/master
Fixed namespace bug and added current state
2 parents 62512a0 + 45efbfc commit b8eb6fc

4 files changed

Lines changed: 47 additions & 20 deletions

File tree

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ $(document).on({
4343
});
4444
```
4545

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+
4654
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`:
4755

4856
```js
@@ -51,9 +59,15 @@ if ($.support.pageVisibility) {
5159
}
5260
```
5361

54-
## Notes
62+
If the Page Visibility API is supported the plugin will also store the current visibility state in `document.hidden`
5563

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
5771

5872
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.
5973

@@ -68,3 +82,4 @@ This plugin is available under the MIT license.
6882
## Contributors
6983

7084
[John-David Dalton](http://allyoucanleet.com/)
85+
[Jan Paepke](http://github.com/janpaepke)

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name" : "jquery-visibility",
3-
"version" : "1.0.7",
3+
"version" : "1.0.8",
44
"main" : "jquery-visibility.js"
55
}

demo.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
}
1010
</style>
1111
<h1>Page Visibility plugin for jQuery</h1>
12-
<p>Detecting Page Visibility API support…
13-
<p><strong>Focus another tab or window</strong>, and watch the log below.
12+
<p>Detecting Page Visibility API support…</p>
13+
<p>The page was <strong id="start-state"></strong> when the page loaded.</p>
14+
<p><strong>Focus another tab or window</strong>, and watch the log below.</p>
1415
<pre></pre>
1516
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
1617
<script src="jquery-visibility.js"></script>
1718
<script>
1819
$(function() {
20+
$("#start-state").text(document.hidden ? "hidden" : "visible");
1921

2022
var $pre = $('pre');
2123

jquery-visibility.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1-
/*! http://mths.be/visibility v1.0.7 by @mathias | MIT license */
1+
/*! http://mths.be/visibility v1.0.8 by @mathias | MIT license */
22
;(function(window, document, $, undefined) {
3+
"use strict";
34

45
var prefix;
56
var property;
67
// In Opera, `'onfocusin' in document == true`, hence the extra `hasFocus` check to detect IE-like behavior
7-
var eventName = 'onfocusin' in document && 'hasFocus' in document
8-
? 'focusin focusout'
9-
: 'focus blur';
8+
var eventName = 'onfocusin' in document && 'hasFocus' in document ?
9+
'focusin focusout' :
10+
'focus blur';
1011
var prefixes = ['webkit', 'o', 'ms', 'moz', ''];
1112
var $support = $.support;
1213
var $event = $.event;
1314

14-
while ((prefix = prefixes.pop()) != undefined) {
15+
while ((prefix = prefixes.pop()) !== undefined) {
1516
property = (prefix ? prefix + 'H': 'h') + 'idden';
16-
if ($support.pageVisibility = typeof document[property] == 'boolean') {
17+
$support.pageVisibility = document[property] !== undefined;
18+
if ($support.pageVisibility) {
1719
eventName = prefix + 'visibilitychange';
1820
break;
1921
}
2022
}
2123

24+
// normalize to and update document hidden property
25+
function updateState() {
26+
if (property !== 'hidden') {
27+
document.hidden = $support.pageVisibility ? document[property] : undefined;
28+
}
29+
}
30+
updateState();
31+
2232
$(/blur$/.test(eventName) ? window : document).on(eventName, function(event) {
2333
var type = event.type;
2434
var originalEvent = event.originalEvent;
@@ -37,19 +47,19 @@
3747
// to check the `relatedTarget` property instead.
3848
if (
3949
!/^focus./.test(type) || (
40-
toElement == undefined &&
41-
originalEvent.fromElement == undefined &&
42-
originalEvent.relatedTarget == undefined
50+
toElement === undefined &&
51+
originalEvent.fromElement === undefined &&
52+
originalEvent.relatedTarget === undefined
4353
)
4454
) {
4555
$event.trigger(
46-
(
47-
property && document[property] || /^(?:blur|focusout)$/.test(type)
48-
? 'hide'
49-
: 'show'
50-
) + '.visibility'
56+
property && document[property] || /^(?:blur|focusout)$/.test(type) ?
57+
'hide' :
58+
'show'
5159
);
5260
}
61+
// and update the current state
62+
updateState();
5363
});
5464

55-
}(this, document, jQuery));
65+
}(this, document, jQuery));

0 commit comments

Comments
 (0)