Reputation: 45
I'm trying to use the following Javascript to redirect user with mobile devices to a certain address.
<script type="text/javascript">
<!--
if (screen.width <= 800) {
window.location = "http://m.domain.com";
}
//-->
</script>
However, when testing it with my smartphone the page won't stop loading (it will load the page repeatedly without showing it - just loading it again and again)
Does anyone have a solution for this problem?
Thanks in advance
Upvotes: 2
Views: 1723
Reputation: 2368
You need also check the URL where now you are like this
<script type="text/javascript">
<!--
if (screen.width <= 800 && !window.location.href.match('http://m.domain.com')) {
window.location.replace("http://m.domain.com");
}
//-->
</script>
Upvotes: 1
Reputation: 13222
You have created an infinite loop...Every time the page loads it will redirect to itself. Only put that javascript
code in your main page not your mobile page.
Upvotes: 1