Skip to content

Instantly share code, notes, and snippets.

@phette23
Created February 3, 2012 00:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phette23/1726749 to your computer and use it in GitHub Desktop.
Save phette23/1726749 to your computer and use it in GitHub Desktop.
Screen-size based redirect
<!-- 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>
@phette23
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment