SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    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:

    Code:
    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?

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You could provide a sub-domain so that mobile devices can access that one instead, such as m.domain.com
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Follow: @AlexDawsonUK silver trophybronze trophy AlexDawson's Avatar
    Join Date
    Feb 2009
    Location
    England, UK
    Posts
    8,111
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    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.

    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.

  5. #5
    SitePoint Member
    Join Date
    May 2010
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW, thanks AlexDawson for the great post! You provided a ton of great insight!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •