A Web Design Community curated by Chris Coyier

Code Snippets Gallery

Code Snippets > JavaScript > Redirect Mobile Devices Submit one!

Redirect Mobile Devices

<script type="text/javascript">
<!--
if (screen.width <= 699) {
document.location = "mobile.html";
}
//-->
</script>

“mobile.html” would be replaced with the location of wherever your mobile version resides. This technique could be adapted to load an alternate stylesheet as well.

For iPhones/iPods Specifically

<script language=javascript>
<!--
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
   location.replace("http://url-to-send-them/iphone.html");
}
-->
</script>

8 Responses

  1. anon says:

    Nice and concise! thanks

  2. Thanks a lot @anon. I find it very useful also.

  3. nLL says:

    What if mobile phone can not use JavaScript?

  4. Fernando Sanches says:

    But and devices that do not work with javascript?

  5. in the iPad/iphone version.

    Is it suppose to say…
    “navigator.userAgent.match(/iPod/i” or
    “navigator.userAgent.match(/iPad/i” ????

  6. Nate Paul says:

    This works great!

    One thing: Once a mobile user logs onto the site it takes them to the mobile site. I want to let them be able to go to the full-version site. But of course when they click on the link, it redirects them to the mobile site.

    Any help is appreciated!

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.