Search code examples
javascriptiphonemobile-safariweb-clips

Determine if user navigated from mobile Safari


I have an app, and I'd like to redirect the users to different pages based on where they are navigating from.

If navigating from web clip, do not redirect. If navigating from mobile Safari, redirect to safari.aspx. If navigating from anywhere else, redirect to unavailable.aspx

I was able to use iPhone WebApps, is there a way to detect how it was loaded? Home Screen vs Safari? to determine if the user was navigating from a web clip, but I'm having trouble determining if the user navigated from mobile Safari on an iPhone or iPod.

Here's what I have:

if (window.navigator.standalone) {
    // user navigated from web clip, don't redirect
}
else if (/*logic for mobile Safari*/) {
    //user navigated from mobile Safari, redirect to safari page
    window.location = "safari.aspx";
}
else {
    //user navigated from some other browser, redirect to unavailable page
    window.location = "unavailable.aspx";
}

Solution

  • UPDATE: This is a very old answer and I cannot delete it because the answer is accepted. Check unwitting's answer below for a better solution.


    You should be able to check for the "iPad" or "iPhone" substring in the user agent string:

    var userAgent = window.navigator.userAgent;
    
    if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
       // iPad or iPhone
    }
    else {
       // Anything else
    }