Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2013
    Posts
    29
    Thanks
    8
    Thanked 0 Times in 0 Posts

    redirect mobile devices to mobile site

    I am using this code to try to redirect mobile devices to a mobile version of site. I put the code in the head section of my html page. Now I am trying it on my ipod touch, and the problem is: If the ipod is held in portrait position, it will redirect, but if the ipod is held in landscape position, it will not redirect, it will show the desktop site. Any idea what's going on here?

    PS I know nothing about js, I found this code on another site.

    Code:
    <script>
    var isMobile = {
        Android: function() {
            return navigator.userAgent.match(/Android/i);
        },
        BlackBerry: function() {
            return navigator.userAgent.match(/BlackBerry/i);
        },
        iOS: function() {
            return navigator.userAgent.match(/iPhone|iPad|iPod/i);
        },
        Opera: function() {
            return navigator.userAgent.match(/Opera Mini/i);
        },
        Windows: function() {
            return navigator.userAgent.match(/IEMobile/i);
        },
        any: function() {
            return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
        }
    };
    
    if (isMobile.any()) {
        // redirect to mobile site
        window.location = "http://www.example.com/m";
    }
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by joejohn View Post
    I am using this code to try to redirect mobile devices to a mobile version of site. I put the code in the head section of my html page. Now I am trying it on my ipod touch, and the problem is: If the ipod is held in portrait position, it will redirect, but if the ipod is held in landscape position, it will not redirect, it will show the desktop site. Any idea what's going on here?
    Whatever cause of the problem, it is nothing to do with the code. Beyond that I haven't a clue.

    We are hoping to see great things coming out of these men's shorts. - Commentator BBC London

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,956
    Thanks
    9
    Thanked 727 Times in 721 Posts
    That sounds as if something is being triggered by screen width rather than mobile detection. Can you give a link to your page?
    Use the W3C HTML Validator and CSS Validator to check your code and Firebug to see what css is applied to an element
    Read Steve Krug's book Don't Make Me Think - essential reading on web usability
    I don't recommend much, but I do recommend Clook for UK web hosting


  •  

    Posting Permissions

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