码迷,mamicode.com
首页 > 移动开发 > 详细

做webapp 使用JS来检测游览器是什么类型,或android是什么版本号

时间:2014-07-12 21:22:55      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:微信游戏   webapp   android   bb10   mobile   

此文转自我的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) > -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的兼容性。

做webapp 使用JS来检测游览器是什么类型,或android是什么版本号,布布扣,bubuko.com

做webapp 使用JS来检测游览器是什么类型,或android是什么版本号

标签:微信游戏   webapp   android   bb10   mobile   

原文地址:http://blog.csdn.net/qazwsx2345/article/details/37723865

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!