Results 1 to 4 of 4

Thread: user agent mobile javascript question (easy question)

Hybrid View

  1. #1
    Join Date
    Jun 2014

    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 language=javascript>

    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {

    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
    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.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Feb 2003
    Michigan, USA
    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.

    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 04:57 PM.

  4. #4
    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.

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