zoukankan      html  css  js  c++  java
  • 做webapp 使用JS来检測游览器是什么类型,或android是什么版本

    此文转自我的www.gbtags.com社区的文章。

    做webapp还是微信游戏,特别是canvas,android上不同机器不同版本号差别还是非常大的。事实上,我今天写了这个js,主要是来做js推断游览器类型,特别使用在android的和widnows phone上的

    1. var brower = {
    2. versions:function(){
    3. var u = window.navigator.userAgent;
    4. var num ;
    5. if(u.indexOf('Trident') > -1){
    6. //IE
    7. return "IE";
    8. }else if(u.indexOf('Presto') > -1){
    9. //opera
    10. return "Opera";
    11. }else if(u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1){
    12. //firefox
    13. return "Firefox";
    14. }else if(u.indexOf('AppleWebKit' && u.indexOf('Safari') > -1) ){
    15. //苹果、谷歌内核
    16. if(u.indexOf('Chrome') > -1){
    17. //chrome
    18. return "Chrome";
    19. }else if(u.indexOf('OPR')){
    20. //webkit Opera
    21. return "Opera_webkit"
    22. }else{
    23. //Safari
    24. return "Safari";
    25. }
    26. }else if(u.indexOf('Mobile') > -1){
    27. //移动端
    28. if(!!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/)){
    29. //ios
    30. if(u.indexOf('iPhone') > -1){
    31. //iphone
    32. return "iPhone"
    33. }else if(u.indexOf('iPod') > -1){
    34. //ipod
    35. return "iPod"
    36. }else if(u.indexOf('iPad') > -1){
    37. //ipad
    38. return "iPad"
    39. }
    40. }else if(u.indexOf('Android') > -1 || u.indexOf('Linux') > -1){
    41. //android
    42. num = u.substr(u.indexOf('Android') + 8, 3);
    43. return {"type":"Android", "version": num};
    44. }else if(u.indexOf('BB10') > -1 ){
    45. //黑莓bb10系统
    46. return "BB10";
    47. }else if(u.indexOf('IEMobile')){
    48. //windows phone
    49. return "Windows Phone"
    50. }
    51. }
    52. }
    53. }

     接下来调用下

    1. brower.versions()

     假设console.log()打印出来就是知道游览器类别了。android的话,会打印出版本,这个主要来做不同android的兼容性。

  • 相关阅读:
    Windos10开机自动 全屏启动 浏览器中的某个 具体网页
    Idea 2019-3.4版本激活码
    CentOS 7安装 MySQL 8.0+ 版本数据库
    CentOS 7更换镜像源
    Linux中 终端、命令、shell解析器 的简单介绍
    angular package.json中start build
    opencv 高级拼接函数Stitcher
    py_faster_rcnn识别出来的结果好多红框重叠
    机器视觉笔记-2
    js获取摄像头视频流
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6884610.html
Copyright © 2011-2022 走看看