Last active
August 29, 2015 14:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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