1
0

jquery-visibility.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*!
  2. * jquery-visibility v1.0.11
  3. * Page visibility shim for jQuery.
  4. *
  5. * Project Website: http://mths.be/visibility
  6. *
  7. * @version 1.0.11
  8. * @license MIT.
  9. * @author Mathias Bynens - @mathias
  10. * @author Jan Paepke - @janpaepke
  11. */
  12. ;(function (root, factory) {
  13. if (typeof define === 'function' && define.amd) {
  14. // AMD. Register as an anonymous module.
  15. define(['jquery'], function ($) {
  16. return factory(root, $);
  17. });
  18. } else if (typeof exports === 'object') {
  19. // Node/CommonJS
  20. module.exports = factory(root, require('jquery'));
  21. } else {
  22. // Browser globals
  23. factory(root, jQuery);
  24. }
  25. }(this, function(window, $, undefined) {
  26. "use strict";
  27. var
  28. document = window.document,
  29. property, // property name of document, that stores page visibility
  30. vendorPrefixes = ['webkit', 'o', 'ms', 'moz', ''],
  31. $support = $.support || {},
  32. // In Opera, `'onfocusin' in document == true`, hence the extra `hasFocus` check to detect IE-like behavior
  33. eventName = 'onfocusin' in document && 'hasFocus' in document ?
  34. 'focusin focusout' :
  35. 'focus blur';
  36. var prefix;
  37. while ((prefix = vendorPrefixes.pop()) !== undefined) {
  38. property = (prefix ? prefix + 'H': 'h') + 'idden';
  39. $support.pageVisibility = document[property] !== undefined;
  40. if ($support.pageVisibility) {
  41. eventName = prefix + 'visibilitychange';
  42. break;
  43. }
  44. }
  45. // normalize to and update document hidden property
  46. function updateState() {
  47. if (property !== 'hidden') {
  48. document.hidden = $support.pageVisibility ? document[property] : undefined;
  49. }
  50. }
  51. updateState();
  52. $(/blur$/.test(eventName) ? window : document).on(eventName, function(event) {
  53. var type = event.type;
  54. var originalEvent = event.originalEvent;
  55. // Avoid errors from triggered native events for which `originalEvent` is
  56. // not available.
  57. if (!originalEvent) {
  58. return;
  59. }
  60. var toElement = originalEvent.toElement;
  61. // If it’s a `{focusin,focusout}` event (IE), `fromElement` and `toElement`
  62. // should both be `null` or `undefined`; else, the page visibility hasn’t
  63. // changed, but the user just clicked somewhere in the doc. In IE9, we need
  64. // to check the `relatedTarget` property instead.
  65. if (
  66. !/^focus./.test(type) || (
  67. toElement === undefined &&
  68. originalEvent.fromElement === undefined &&
  69. originalEvent.relatedTarget === undefined
  70. )
  71. ) {
  72. $(document).triggerHandler(
  73. property && document[property] || /^(?:blur|focusout)$/.test(type) ?
  74. 'hide' :
  75. 'show'
  76. );
  77. }
  78. // and update the current state
  79. updateState();
  80. });
  81. }));