csshttp-redirectmobile

What is the best way to do a mobile redirect?


I'm sure this gets asked a lot, but I also know things change quite frequently, so I am wondering what the current best way to do a simple mobile detection and redirection for a site is.

I'm not going to be able to cover all phones, but I'd like to at least get iPhones and Androids.


Solution

  • <script type="text/javascript">
        function checkBrowser(){
            var str=navigator.appVersion; 
            var patt1=/Android/;
            var patt2=/iPhone/;
            if(str.match(patt1) || str.match(patt2)){
                window.location="http://m.yoursite.com";
            }
        }
    </script>
    

    then on the body-tag put onload="checkBrowser();"

    it's a very simple way to do it, but i'm sure there plenty more ways.. hope it helps :)