Reputation: 691
I have a website and a mobile version of that website and I want to use something to redirect users based upon their browser size. At the moment I have:
<script>
if (screen.width <= 1024) window.location.replace("http://www.website.com/")
else window.location.replace("http://m.website.com/")
</script>
This works fine for redirecting to home pages but what about linking to specific pages based on external income sources? For example if someone reads my tweet about my latest blog which is at http://www.website.com/post.php?id=1111
, how do I ensure that mobile users are redirected to http://m.website.com/post.php?id=1111
?
Upvotes: 1
Views: 83
Reputation: 4843
Try something like this:
var reg = "www.website.com";
var mob = "m.website.com";
if(screen.width >= 1024) {
if(window.location.hostname != reg)
window.location.replace("http://" + reg + window.location.pathname + window.location.search)
}
else {
if(window.location.hostname != mob)
window.location.replace("http://" + mob + window.location.pathname + window.location.search)
}
Upvotes: 1