Home Services Tutorials Articles Resources Contact Us

JavaScript Auto-Bowser Detection


When choosing how to identify the browser settings of users visiting your site, it is usually not advisable to use user agent detection, though you may want to consider this option if you are developing an iPhone/iPod touch specific microsite or web app which needs MobileSafari-specific capabilities. The following JavaScript will accomplish this task and forward your user to the iPhone specific version of your webiste.

<script language=javascript>
if((navigator.userAgent.match(/iPhone/i))||
(navigator.userAgent.match(/iPod/i)))
{
location.replace("iPhone-version.html");
}
</script>

Just paste this into the head of your main sites homepage and replace “iPhone-version.html” with the path to your iPhone specific html file. When a user visits your site from an iPhone or iPod touch they will be directed to your the page you specified.

You can download a zip of the html file here. We hope you find this example useful, let us know what you think.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

3 Comments so far »

  1. Comment by Erik: July 15, 2008 @ 8:25 pm

    Worked perfectly the first time! Thanks.

  2. Comment by Dmoss: July 22, 2008 @ 1:12 pm

    How do you do the opposite? Like… if someone tries to visit my “website.com/iphone” with their Firefox browser, I’d like to prevent them from seeing that page. Perhaps redirect them to my non-iPhone page.

  3. Comment by admin: July 23, 2008 @ 4:36 pm

    Hi Dmoss,

    One way is to check that the user agent is not equal to iPhone/iPod, and forward accordingly.

    if((!navigator.userAgent.match(/iPhone/i))&&
    (!navigator.userAgent.match(/iPod/i)))
    {
    location.replace(”regular-version.html”);
    }

    This would go on your main iPhone site. Hope that helps!

    -Admin

Comment RSS · TrackBack URI

47