Is there a way to detect if a browser window is not currently active?

Since originally writing this answer, a new specification has reached working draft status thanks to the W3C. The Page Visibility API now allows us to more accurately detect when a page is hidden to the user.

Current browser support:

  • Chrome 13
  • Internet Explorer 10
  • Firefox 10
  • Opera 12.10 [read notes]

The following code makes use of the API, falling back to the less reliable blur/focus method in incompatible browsers.

onfocusin and onfocusout are required for IE 9 and lower, while all others make use of onfocusand onblur, except for iOS, which uses onpageshow and onpagehide.




电子邮件地址不会被公开。 必填项已用*标注