There was an error in this gadget

Monday, January 22, 2007

Two Scripts to Detect Mobile Browsers

When writing mLearning content, sometimes it is good to have a single URL that all your participants can access. The following are two scripts that you can add into the head content of your page that will detect a mobile browser and automatically redirect your users to a 'mobile friendly' version of your site.

_____________________________________________

Script #1

In this script, change the $location attribute to the page you want your users to see if they are using one of the mobile devices listed in the if(stristr.. string.
____________________________________________________


Script #2


This is a simpler script to detect Windows CE browsers (although you can detect any browser with this script.) Simply change the window.location URL to your mobile friendly site.


____________________________________________________




In both of these cases, I'm redirecting the site to the mobile version of this site- http://www.2mlearn.mobi/


I will be adding this script to http://www.mlearning-world.com/ in the next few days
to help all our visitors with mobile devices.



8 comments:

Leonard said...

Hey Matthew; good, useful post, but I'm wondering about the purpose of the second conditional in the first set of code. Couldn't you optimise it thus?

if (stristr($ua, "Windows CE") or stristr($ua, "AvantGo") or stristr($ua, "Mazingo") or stristr($ua, "Mobile") or stristr($ua, "T68") or stristr($ua, "Syncalot") or stristr($ua, "Blazer"))
{ $location='http://winksite.com/mlearning/teach';
header('Location: '.$location);
}

This would remove the second conditional and make it a bit simpler.

Cheers,
L.
http://mlearning.edublogs.org

Matthew Nehrling said...

Good catch Leonard. I will test it both ways with several devices to see if the DeviceType conditional is needed.

Mike said...

great script. I'm using

(script language='JavaScript' type="text/JavaScript">
var isCE = navigator.appVersion.indexOf("Windows CE")>0;
if (isCE)
{
window.location.href="http://m.fordpiano.com";
}
(/script)
w/ minor adjustments

How do you go about modifying the script so that it will detect other devices such as a black berry, iphone, etc. Thanks!

Dave said...

another alternative would be to use a service like handsetdetection.com

website design nyc said...

nice post

Eric said...

Nice posting... easy to download

satuiku said...

i like this articlee

Mike said...

I've created a similar set of scripts in an article I wrote entitled Mobile Browser Detection and Redirection with PHP... Here's a link: http://www.mlynn.org/2010/06/mobile-device-detection-and-redirection-with-php/

There is a link for downloading the example scripts.

Enjoy!
Mike