Skip to content

Instantly share code, notes, and snippets.

@ckhatton
Last active August 29, 2015 14:11
Show Gist options
  • Save ckhatton/c9b20dae06450d78e059 to your computer and use it in GitHub Desktop.
Save ckhatton/c9b20dae06450d78e059 to your computer and use it in GitHub Desktop.
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';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment