www.webdeveloper.com
Results 1 to 4 of 4

Thread: user agent mobile javascript question (easy question)

  1. #1
    Join Date
    Jun 2014
    Posts
    1

    user agent mobile javascript question (easy question)

    I know no javascript, so this is a basic question. I'm using this code to send visitors to certain pages if they are on android/ios phones:

    <script type="text/javascript"> // <![CDATA[
    if ( (navigator.userAgent.indexOf('Android') != -1) ) {
    document.location = "mobileandroid.php";
    } // ]]>
    </script>

    <script language=javascript>

    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    location.replace("mobileios.php");
    }

    </script>
    --
    How do i write the code for "else if" (all the users who are not using ios/android, including pc users) as in else if all visitors go to http://www.anothersite.com

    thank you for helping me out with this basic question.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    You would be better off attempting this server side BUT also note that smartphones these days come with web browsers that support HTML in full.

    It is a personal hate of mine when a site detects I am a smart phone and pushes me on to a mobile (simpler) interface.

    If you must have a mobile website, at least check if the user wants to use a full HTML site or try to ascertain the level of support and direct accordingly.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    You may be better off diving into Responsive Design, which negates the need for a mobile web site. If you really do need to detect mobile browsers, say because you are building a complex web application, then I agree with \\.\ that you need a server side solution.

    You may also want to use the window.matchMedia function to test for a certain viewport size, and if it is too small, display a note to the user. Otherwise, initialize your web application.

    Code:
    if (matchMedia("screen and (min-width: 800px)").matches) {
        // ... all is good. Initialize your application
    }
    else {
        // ... tell the user all is not good and provide a link to the mobile site
    }
    Last edited by toicontien; 06-09-2014 at 03:57 PM.

  4. #4
    Join Date
    May 2014
    Posts
    909
    You're getting some good advice here so far -- I would add that the very NOTION of what you are asking is broken. What you have there is browser and/or device sniffing, something you have ZERO business doing on a website in the first place. It most always breaks sooner than later, and it fails to take into account the ENTIRE reason HTML exists...

    Device neutral delivery of content.

    IF you have your HTML written properly, with proper separation of presentation from content, using an elastic semi-fluid layout, it's not rocket science to re-arrange the layout to best fit the capabilities (usually width) of the device it's being shown on using media queries -- the so called "responsive layout".

    Said media queries should be based on the needs of the content, NOT the device. Thinking specific devices is a waste of time because there are ENDLESS devices, there will be all sorts of new devices coming along -- you can't plan for all that. All you can plan for is having the content auto-adjust to fit whatever space is available.

    Sniffing the UA string and specific designs for specific devices? No future-proofing, way more work, and on the whole a complete waste of time and effort.
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles