1

Within a setTimeout, I am setting the location of a newly opened window. To avoid the pop-up blocker, the window is opened with a temporary local URL, and then location is changed to the target URL. This works fine on all desktop browsers including Safari.

However, in Mobile Safari, the location of the new window is not updated until the user returns their focus to the originating window i.e. the temporary page is left sitting there.

This is what the relevant bit of the JavaScript/jQuery looks like:

var self = $(this);
if (self.is("a")) {
    var clickEventHandler = function(event) {
        event.preventDefault();
        if (self.attr("target")) {
            var newWindow = window.open('loading.htm',self.attr("target"));
            var externalURL=self.attr("href");
            setTimeout( function() { newWindow.location=externalURL; }, 100 );
        }
    }
    $(this).bind("click", eventHandler);
}
mmorrey
  • 103
  • 1
  • 5
  • Why do you need to do this within `setTimeout`? – Michael Mior Dec 19 '12 at 16:36
  • I'd guess that mobile Safari defers `setTiemout` until focus is returned. What happens for you do `document.body.innerHTML += new Date().toLocaleString()` in a `setTimeout`? What time does it report when you return from a different window? – apsillers Dec 19 '12 at 16:40
  • @Michael-Mior This approach is needed because I am tracking the event in Google Analtyics, and this will not work without introducting a short delay, [see this SO thread](http://stackoverflow.com/questions/3823489/how-does-google-analytics-track-events-when-user-navigates-to-other-page-inside) – mmorrey Dec 19 '12 at 17:09
  • @apsillers yes, exactly that. I tried your test, and it reports the time that I returned to the original window. I'll put a test page up publicly somewhere. – mmorrey Dec 19 '12 at 17:47
  • Apparentely, this is a [known problem with Mobile Safari](http://stackoverflow.com/questions/11795864/pagehide-event-on-imminent-tab-switching-in-mobile-safari-does-not-fire-when-run) – mmorrey Jan 22 '13 at 12:47
  • There are some [test pages for this problem linked in a related SO post](http://stackoverflow.com/questions/7977170/ios-5-pauses-javascript-when-tab-is-not-active) – mmorrey Jan 22 '13 at 12:51

0 Answers0