Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile Safari/Webkit On Window Focus Event

I am wondering if it is possible to get a event when the user switches back to "your" page's window? This could happen either when the user opens a new tab and then switches back to your page's tab or when the user closes safari and then opens it again.

I want to be able to update content on the page once I get this event. I am using a setInterval to accomplish this now, but it would be nice to not have the delay when the user focuses on your window.

Thanks!

like image 252
joneath Avatar asked Oct 01 '10 04:10

joneath


People also ask

What is window focus event?

The focus event fires when an element has received focus. The opposite of focus is blur . This event is not cancelable and does not bubble.

How do you check if a window is focused?

Use the document. hasFocus() method to check if a window has focus, e.g. if (document. hasFocus()) {} . The method returns a boolean value indicating whether the document or any element in the document has focus.

What is focus event?

The focus event fires when an element has received focus. The event does not bubble, but the related focusin event that follows does bubble. The opposite of focus is the blur event, which fires when the element has lost focus. The focus event is not cancelable.


2 Answers

The chance of this one getting resolved seems small:

I created this script to log as many WebKit events as possible (event names taken from http://svn.webkit.org/repository/webkit/trunk/Source/WebCore/dom/EventNames.h), and it doesn't seem to trigger any event upon leaving/bringing to front Safari.

like image 155
Jacob Oscarson Avatar answered Oct 19 '22 03:10

Jacob Oscarson


I've been digging around and found that a focus event is fired when the window is activated again.

It should be doable like this (using jQuery):

$(window).on('focus', function() {
    ...focus code here...
});

This event is probably fired more often than needed, but it worked for me.

like image 25
mzedeler Avatar answered Oct 19 '22 04:10

mzedeler