Topic: Redirect to Mobile with JavaScript
Share/Save/Bookmark
Description: This script will show you how to catch the device and redirect to a mobile version of your website.
Notes: This was taked from the source of the following site: http://www.porsche.com/usa/

Script is shown below

<script type="text/javascript">
<!--
var
arrMobileClients=[
   "iphone"
,
   "ipod"
/*,
   "240x320",
   "benq",
   "blackberry",
   "mda",
   "midp",
   "mot-",
   "netfront",
   "nokia",
   "opera mini",
   "panasonic",
   "philips",
   "pocket pc",
   "portalmmm",
   "sagem",
   "samsung",
   "sda",
   "sgh-",
   "sharp",
   "sie-",
   "sonyericsson",
   "symbian",
   "vodafone",
   "windows ce",
   "xda"*/
];

function isMobileNavigator(userAgent)
{
   try

   {
      userAgent=userAgent.toLowerCase();
      for (var
i=0; i < arrMobileClients.length; i++)
      {
         if
(userAgent.indexOf(arrMobileClients[i]) != -1)
         {
            return true
;
         }
      }
   }
   catch
(e)
   {
      // NOP
   }
   return false
;
}

function goMobile(URL)
{
   var params = URL.split("?"
);
   if
(params.length == 2)
   {
      if(params[1].indexOf("keephost=true"
) != -1)
      return false
;
   }
   else if(URL.indexOf("mobile."
) != -1)
   {
      return false
;
   }
   return true
;
}

var arrMobilePools=["usa"
];
function
isMobilePool()
{
   /*
   try
   {
      for (var i=0; i < arrRedirectHosts.length; i++)
      {
         if (CURRENTPOOL == arrRedirectHosts[i])
         {
            return true;
         }
      }
   }
   catch (e)
   {
      // NOP
   }
   */

   return true
;
}

function
mobileRedirector()
{
   try

   {
      if
(isMobileNavigator(navigator.userAgent))
      {
         if
(isMobilePool())
         {
            //alert(navigator.userAgent);
            var
URL = document.location.href.toLowerCase();
            if
(goMobile(URL))
            {
               URL = URL.replace(
"http://www.", "http://mobile."
);
               URL = URL.replace(
"https://preview.", "https://preview.mobile."
);
               document.location.href = URL;
            }
         }
      }
   }
   catch
(e)
   {
      // NOP
   }
}

// check whether to continue loading or redirect to the mobile URL.
mobileRedirector();
-->
</script>