Created
February 3, 2012 00:34
-
-
Save phette23/1726749 to your computer and use it in GitHub Desktop.
Screen-size based redirect
This file contains hidden or 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
<!-- redirect based on screen width --> | |
<script type="text/javascript"> | |
console.log('Screen width: '+screen.width); | |
if (screen.width <= 699 && !sessionStorage.getItem("redirecting")) { | |
window.location.replace("./m/index.html"); //path to your mobile site | |
} | |
</script> | |
<!-- redirect bypass on the mobile site | |
such that users don't get caught in a redirect loop | |
uses jQuery cuz mobile site is jQuery Mobile | |
but could easily be rewritten --> | |
<script> | |
$('a[href="http://example.com/"]').click(function(){sessionStorage.setItem("redirecting","1")}); | |
//where example.com = your desktop site | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: window.location.replace(url) is clearly more appropriate for JS redirects since it doesn't leave the redirecting page in the browser's history. See MDN's article on window.location.