Will Jamieson
Will Jamieson

Reputation: 926

JavaScript not detecting mobile device and redirecting

I want to be able to determine if the device is an android or an iphone and then redirect it accordingly. However when I test it it does not work It just loads the html file. Any ideas why this does not work? Also I really have no experience with java script. I stick mainly to mobile dev. I opened the url on an iphone and android and had no luck with either.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <script>
         var ua = navigator.userAgent.toLowerCase();
         var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
         if (isAndroid) {
            // Do something!
            // Redirect to Android-site?
            window.location = 'http://espn.go.com/';
         }

         if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
            if (document.cookie.indexOf("iphone_redirect=false") == - 1) window.location = "http://www.yahoo.com/;
         }

      </script>
      <title>Redirect</title>
      <script>
         // google analytic code is here
      </script>

         Redirecting 
      </body>
</html>

Upvotes: 0

Views: 115

Answers (2)

lviggiani
lviggiani

Reputation: 6066

Set document.location instead of window.location.

document.location = "http://google.com"

UPDATE

actually, you can still use window.location, you just missed the trailing " in your url:

BAD: window.location = "http://google.com;
GOOD: window.location = "http://google.com";

Upvotes: 3

Igor Benikov
Igor Benikov

Reputation: 897

You just have syntax error:

window.location = "http://www.yahoo.com/;

Upvotes: 3

Related Questions