当前位置:   article > 正文

判断是pc还是移动浏览器_移动端浏览链接请求头怎么写才会识别是移动端

移动端浏览链接请求头怎么写才会识别是移动端
  1. //把请求头信息转为小写
  2. //user agent是指用户代理,使服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。
  3. sUserAgent = navigator.userAgent.toLowerCase();
  4. sUserAgent.match('ipad') == "ipad";
  5. sUserAgent.match('ucweb') == "ucweb";
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>判断是PC还是移动端浏览器</title>
  7. </head>
  8. <body>
  9. </body>
  10. </html>
  11. <script>
  12. function browserRedirect() {
  13. var curURL = window.location.href;
  14. var sUserAgent = navigator.userAgent.toLowerCase();
  15. var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  16. var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  17. var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  18. var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  19. var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  20. var bIsAndroid = sUserAgent.match(/android/i) == "android";
  21. var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  22. var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  23. if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
  24. // 移动端浏览器
  25. document.write("phone");
  26. //移动端浏览器
  27. window.location.href = "https://m.jd.com/";
  28. // if (curURL.indexOf("jd.com") != -1) {
  29. // window.location.href = "https://m.jd.com/";
  30. // }
  31. } else {
  32. // PC端浏览器
  33. document.write("pc");
  34. // if (curURL.indexOf("jd.com") != -1) {
  35. // window.location.href = "https://www.jd.com/";
  36. // }
  37. }
  38. }
  39. browserRedirect()
  40. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/704931
推荐阅读
相关标签
  

闽ICP备14008679号