6

Is there a cross browser event that can be used to show a message to the user returning to their web page?

For example, a user has ten applications or tabs open. They get a new notification from our app and I show a notification box. When they switch to our tab I want to begin our notification animation.

The activate event is common on desktop applications but so far, on the window, document and body, neither the "activate" or "DOMActivate" do anything when swapping between applications or tabs but the "focus" and "blur" do. This event works but the naming is different and the events that should be doing this are not.

So is the right event to use cross browser or is there another event?

You can test by adding this in the console or page and then swapping between applications or tabs:

window.addEventListener("focus", function(e) {console.log("focused at " + performance.now()) } ) 
window.addEventListener("blur", function(e) {console.log("blurred at " + performance.now()) } )

Update:
In the link to the possible duplicate is a link to the W3 Page Visibility doc here.

It says to use the visibilitychange event to check when the page is visible or hidden like so:

document.addEventListener('visibilitychange', handleVisibilityChange, false);

But there are issues:

The Document of the top level browsing context can be in one of the following visibility states:

hidden The Document is not visible at all on any screen. visible The Document is at least partially visible on at least one screen. This is the same condition under which the hidden attribute is set to false.

So it explains why it's not firing when switching apps. But even when switching apps and the window is completely hidden the event does not trigger (in Firefox).

So at the end of the page is this note:

The Page Visibility API enables developers to know when a Document is visible or in focus. Existing mechanisms, such as the focus and blur events, when attached to the Window object already provide a mechanism to detect when the Document is the active document.

So it would seem to suggest that it's accepted practice to use focus and blur to detect window activation or app switching.

I found this answer that is close to what would be needed to make a cross browser solution but needs focus and blur (at least for Firefox).

Observation:
StackOverflow has a policy against mentioning frameworks or libraries. The answers linked here have upvotes for the "best" answer.

But these can grow outdated. Since yesterday I found mention of two frameworks (polyfills) that attempt to solve this same problem here for visibly and isVis (not creating a link). If this is a question and answer site and a valid answer is, "here is some code that works for me" but "Here is the library I created using the same code that can be kept up to date and maintained on github" is not valid then in my opinion it's missing it's goal.

I know above should probably go to meta and I have but they resist changing the status quo for some reason. Mentioning it here since it's a relevant example.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • This is one way of doing it by using focus and blur, yes. Please see link below. – Alexandre Elshobokshy Dec 13 '18 at 09:59
  • Possible duplicate of [Is there a way to detect if a browser window is not currently active?](https://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active) – Alexandre Elshobokshy Dec 13 '18 at 09:59
  • My question is different in that it is to check if the window is currently active not inactive. See the difference? I care if they activate the window not if it is deactive. However a lot of that answer is helpful. – 1.21 gigawatts Dec 13 '18 at 11:36

1 Answers1

3

The Page lifecycle API can be used to listen for visibilitychange events.

[This event triggers] when a user navigates to a new page, switches tabs, closes a tab, minimizes or closes the browser, or switches apps on mobile operating systems. Quote

Current browser support

Reference on MDN

Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38
  • Thank you. I read through it and then tested it and it works for tab switching but doesn't seem to pick up events from application switching (via alt + tab). Focus and blur seem to work at least in Firefox so a combination might work. Another note: when switching and the page is completely occluded the document.hidden flag still reports visible. – 1.21 gigawatts Dec 13 '18 at 11:38