Guest User

Untitled

a guest
Oct 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Page Visibility API</title>
  6. <meta name="viewport" content="width=device-width">
  7. </head>
  8. <body>
  9. <h1>Page Visibility API</h1>
  10. <p>As long as this tab is the one tab in the browser with focus then nothing happens. When the user switches to another tab that triggers the visibilitychange event on the document object.</p>
  11. <script>
  12. //document.hidden
  13. //document.addEventListener('visibilitychange', myfunc)
  14. //... webkitHidden msHidden
  15. //... webkitvisibilitychange msvisibilitychange
  16. let hidden, vChange;
  17. if( typeof document.hidden !== 'undefined'){
  18. hidden = 'hidden';
  19. vChange = 'visibilitychange';
  20. }else if(typeof document.webkitHidden !== 'undefined'){
  21. hidden = 'webkitHidden';
  22. vChange = 'webkitvisibilitychange';
  23. }else if(typeof document.msHidden !== 'undefined'){
  24. hidden = 'msHidden';
  25. vChange = 'msvisibilitychange';
  26. }else{
  27. //no support
  28. hidden = null;
  29. vChange = null;
  30. }
  31. if( hidden !== null){
  32. document.addEventListener(vChange, function(ev){
  33.  
  34. console.log('visibilitychange', document[hidden]);
  35.  
  36. if(document[hidden]){
  37. //page has lost focus
  38. //stop the audio or video
  39. }else{
  40. //page has regained focus
  41. //start the audio or video
  42. }
  43. });
  44. }
  45. </script>
  46. </body>
  47. </html>
Add Comment
Please, Sign In to add comment