ckhatton
12/9/2014 - 5:45 PM

Redirect the user to a mobile or desktop version of the page, by detecting the user agent. Also shows the desktop version if the user has cl

Redirect the user to a mobile or desktop version of the page, by detecting the user agent. Also shows the desktop version if the user has clicked to view the desktop version only (the cookie lasts an hour). Make sure to include the 'jquery.cookie.js' file somewhere above the script.

$(document).ready(function(){
  $('.view-desktop-version')
  .click(function(){
    var date = new Date();
    date.setTime(date.getTime() + (60 * 60 * 1000));
    $.cookie('page_ver', 'desktop', { expires: date, path: '/' });

    window.location = '/desktop';
  });
});

if ( navigator.userAgent.match(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i) ) {
  if ( screen.width <= 640 ) { // Accounts for the size range of Androids and other devices
    if ( window.location.pathname != '/pages/mobile' && !$.cookie('pg_ver')) window.location = '/pages/mobile';
    if ( window.location.pathname == '/pages/mobile' && $.cookie('pg_ver')) window.location = '/pages/desktop';
  } else {
    if ( window.location.pathname != '/pages/desktop') window.location = '/pages/desktop';
  }
} else {
  if ( window.location.pathname != '/pages/desktop') window.location = '/pages/desktop';
}