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的兼容性。

  • 相关阅读:
    bzoj4321
    bzoj1800
    codeforces396C
    codeforces400C
    codeforces271D
    关于jsp中jstl-core标签循环遍历的使用
    hibernate坑边闲话2
    hibernate坑边闲话
    hibernate中实体与数据库中属性对应的类型
    MySQL的常用命令:添加外键,修改字段名称,增加字段 设置主键自增长等
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6884610.html
Copyright © 2011-2022 走看看