Fabian van Herer
Fabian van Herer

Reputation: 45

Using the Javascript window.location Method to redirect user with mobile devices

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

Answers (2)

MysterX
MysterX

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

brso05
brso05

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

Related Questions