How to add to your site redirect code to mobile site

Goal:

Need to determine that the user visited the site from a mobile device, and if yes, then redirect him to "http://<mobile_site>".

"<mobile_site>" will be "<Your_domain>.storemobilizer.com".

Also supported sub-domain for your domain, for example: "m.<Your domain>." (Sub-domain must be configured in your DNS as an alias / CNAME for "<Your_domain>.storemobilizer.com").

Solution:

You need to add JavaScript code in the "<head>" section of the site.

Method 1:

<head>
  ..
  <script>
  if('ontouchstart' in window || navigator.msMaxTouchPoints > 0)
    location.href = 'http://<mobile_site>';
  </script>
  ..
</head>

Advantages of this method - simplicity. Disadvantage - mobile users will always be redirected to the mobile site.

To solve this we offer Method 2:

<head>
  ..
  <script>
  (function(ALS){
  if(!ALS)return;
  if(ALS['mobile_state'] != 'not_redirect')
    ALS['mobile_state'] =
    (('ontouchstart' in window) || (navigator.msMaxTouchPoints > 0))
    ? 'redirect' : 'not_mobile';
  if((location.hash == '#mobile=no')&&(ALS['mobile_state'] == 'redirect'))
    ALS['mobile_state'] = 'not_redirect';
  if(ALS['mobile_state'] == 'redirect')
    location.href = 'http://<mobile_site>';
  })(window.localStorage);
  </script>
  ..
</head>

Method 2 supports url parameter "#mobile=no", which allows to disable the automatic redirection. Variable "mobile_state" is used, which is stored in localStorage.