ylsunyuan技术论坛

标题: JS判断浏览器是否为移动端 [打印本页]

作者: sun    时间: 2016-4-29 23:07
标题: JS判断浏览器是否为移动端
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4.         <title>手机首页</title>
  5. </head>
  6. <body>
  7. <h1>手机首页</h1><hr>
  8. <script>

  9.     if(navigator.platform.indexOf('Win32')!=-1){

  10.     //pc

  11.         document.write("PC");

  12.      }else{

  13.    //shouji

  14.         //window.location.href="手机网址";
  15.         document.write("Phone");

  16.       }

  17. </script>

  18. <!-- 判断移动终端 -->
  19. <script type="text/javascript">

  20. var mobileAgent = new Array("iphone", "ipod", "ipad", "android", "mobile", "blackberry", "webos", "incognito", "webmate", "bada", "nokia", "lg", "ucweb", "skyfire");

  21. var browser = navigator.userAgent.toLowerCase();

  22. var isMobile = false;

  23. for (var i=0; i<mobileAgent.length; i++){

  24.         if (browser.indexOf(mobileAgent[i])!=-1){
  25.                 isMobile = true;
  26.                 alert(mobileAgent[i]);
  27.                 //location.href = '手机网址';
  28.                 break;
  29.         }
  30. }

  31. </script>
  32. </body>
  33. </html>
复制代码







欢迎光临 ylsunyuan技术论坛 (http://bbs.ylsunyuan.com/) Powered by Discuz! X3.2