JavaScript没有检测到移动设备和重定向

时间:2013-12-30 19:42:04

标签: javascript html

我希望能够确定设备是Android还是iphone,然后相应地重定向。但是当我测试它时它不起作用它只是加载html文件。任何想法为什么这不起作用?另外,我真的没有使用java脚本的经验。我主要坚持移动开发。我在iPhone和Android上打开了网址,但也没有运气。

<!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>

2 个答案:

答案 0 :(得分:3)

设置document.location而不是window.location

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

<强>更新

实际上,您仍然可以使用window.location,但您错过了网址中的"

坏:window.location = "http://google.com;
好的:window.location = "http://google.com";

答案 1 :(得分:3)

您只有语法错误:

window.location = "http://www.yahoo.com/;
相关问题