Skip to content

Instantly share code, notes, and snippets.

@greeno
Created May 8, 2013 16:36
Show Gist options
  • Save greeno/5541707 to your computer and use it in GitHub Desktop.
Save greeno/5541707 to your computer and use it in GitHub Desktop.
Mobile Device Page Redirection
<!-- Normal Non-Mobile Page -->
...
<script src="../js/cookie.js"></script>
...
$(function(){
console.log($(window).width());
console.log(readCookie("forceStandardView"));
// 1024 is a random choice. What should it be? Should iPad use mobile view? Only 480<?
// Research width. Does device-width == window.width? Or at least close?
if($(window).width() < 1024 && readCookie("forceStandardView")!="yes"){
window.location.replace("first_page.html");
}
})
...
<a href="first_page.html">Switch to mobile version</a>
...
<!-- Mobile Page -->
...
<script src="../js/cookie.js"></script>
...
$("#forceStandardView").bind("click",function(e){
console.log("forceStandardView");
createCookie("forceStandardView","yes",14);
})
...
<a href="index.html" id="forceStandardView" data-role="button" rel="external">Go to full site</a>
<!-- cookie.js -->
/* Cookie Handeling */
function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else var expires = "";
document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = escape(name) + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length));
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment