Mobile redirect, but not for iPad script

I am trying to write a script that will redirect for all mobile devices (iPhone, iPod Touch, Android, BlackBerry, webOS, etc), but not for the iPad, its large screen makes it ideal for web browsing. I can’t simply write something like:



if (navigator.userAgent.indexOf("Mobile") != -1) {
		    window.location = "http://www.mydomain.com/mobile"
			}
else {
	window.location = "http://www.mydomain.com/standard"
}


because the iPad’s userAgent has the word Mobile in it:


Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

I know that I could just list out every device in the if statement, but what upcoming mobile devices such as Windows Phone 7. I don’t want to keep adding devices to the list. I just want to set it once for all mobile devices and it be done with it.

Does anyone have any ideas?

SmallWhiteDragon, don’t bother trying to redirect everyone. All of the solutions that exist are poorly implemented and the fact many mobile devices use the SAME user-agent string as desktop browsers makes any such redirection an impossibility. All you’re going to-do is end up with a gnarly script which is dependant on scripting, causes pages to load excessively slowly (due to server-side processing of the 250,000 or so user-agents for mobile devices) and being perfectly frank. Why are you making decisions for your end users? Have a link to the mobile site by all means in the head of the document, but don’t force people (like iPhone users) to use the version you dictate, that’s a deprecated practice like the “best viewed in” crap everyone suffered in the 90’s or those bad redirects based on the persons resolution. Your job is to let people know a mobile version of the site is possible, not to tell them that they MUST use the version of the site you use… contrary to popular myth, many smartphones CAN view full size websites with little problem (using stuff like panel / progessive zooming). Let your visitors pick the experience the want. :slight_smile:

PS: Around 10 new mobile devices are released (worldwide) every week, so if you do redirect people based on user-agent string, you’ll be spending all your time updating the script and it’ll become so bulky due to the sheer number of devices out there, you’ll waste precious resources on building a library of user-agents.

Would it not be more effective just to redirect based on the user’s screen resolution? (i.e - if the screen resolution is less than 800x600 then redirect).

Also it’s always a good idea to have the same functionality present in the backend as well in case the user doesn’t have javascript enabled.

Good luck

You could provide a sub-domain so that mobile devices can access that one instead, such as m.domain.com

WOW, thanks AlexDawson for the great post! You provided a ton of great insight!