sniff.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. define(["./has"], function(has){
  2. // module:
  3. // dojo/sniff
  4. /*=====
  5. return function(){
  6. // summary:
  7. // This module sets has() flags based on the current browser.
  8. // It returns the has() function.
  9. };
  10. =====*/
  11. if(has("host-browser")){
  12. var n = navigator,
  13. dua = n.userAgent,
  14. dav = n.appVersion,
  15. tv = parseFloat(dav);
  16. has.add("air", dua.indexOf("AdobeAIR") >= 0);
  17. has.add("msapp", parseFloat(dua.split("MSAppHost/")[1]) || undefined);
  18. has.add("khtml", dav.indexOf("Konqueror") >= 0 ? tv : undefined);
  19. has.add("webkit", parseFloat(dua.split("WebKit/")[1]) || undefined);
  20. has.add("chrome", parseFloat(dua.split("Chrome/")[1]) || undefined);
  21. has.add("safari", dav.indexOf("Safari")>=0 && !has("chrome") ? parseFloat(dav.split("Version/")[1]) : undefined);
  22. has.add("mac", dav.indexOf("Macintosh") >= 0);
  23. has.add("quirks", document.compatMode == "BackCompat");
  24. if(dua.match(/(iPhone|iPod|iPad)/)){
  25. var p = RegExp.$1.replace(/P/, "p");
  26. var v = dua.match(/OS ([\d_]+)/) ? RegExp.$1 : "1";
  27. var os = parseFloat(v.replace(/_/, ".").replace(/_/g, ""));
  28. has.add(p, os); // "iphone", "ipad" or "ipod"
  29. has.add("ios", os);
  30. }
  31. has.add("android", parseFloat(dua.split("Android ")[1]) || undefined);
  32. has.add("bb", (dua.indexOf("BlackBerry") >= 0 || dua.indexOf("BB10") >= 0) && parseFloat(dua.split("Version/")[1]) || undefined);
  33. has.add("trident", parseFloat(dav.split("Trident/")[1]) || undefined);
  34. has.add("svg", typeof SVGAngle !== "undefined");
  35. if(!has("webkit")){
  36. // Opera
  37. if(dua.indexOf("Opera") >= 0){
  38. // see http://dev.opera.com/articles/view/opera-ua-string-changes and http://www.useragentstring.com/pages/Opera/
  39. // 9.8 has both styles; <9.8, 9.9 only old style
  40. has.add("opera", tv >= 9.8 ? parseFloat(dua.split("Version/")[1]) || tv : tv);
  41. }
  42. // Mozilla and firefox
  43. if(dua.indexOf("Gecko") >= 0 && !has("khtml") && !has("webkit") && !has("trident")){
  44. has.add("mozilla", tv);
  45. }
  46. if(has("mozilla")){
  47. //We really need to get away from this. Consider a sane isGecko approach for the future.
  48. has.add("ff", parseFloat(dua.split("Firefox/")[1] || dua.split("Minefield/")[1]) || undefined);
  49. }
  50. // IE
  51. if(document.all && !has("opera")){
  52. var isIE = parseFloat(dav.split("MSIE ")[1]) || undefined;
  53. //In cases where the page has an HTTP header or META tag with
  54. //X-UA-Compatible, then it is in emulation mode.
  55. //Make sure isIE reflects the desired version.
  56. //document.documentMode of 5 means quirks mode.
  57. //Only switch the value if documentMode's major version
  58. //is different from isIE's major version.
  59. var mode = document.documentMode;
  60. if(mode && mode != 5 && Math.floor(isIE) != mode){
  61. isIE = mode;
  62. }
  63. has.add("ie", isIE);
  64. }
  65. // Wii
  66. has.add("wii", typeof opera != "undefined" && opera.wiiremote);
  67. }
  68. }
  69. return has;
  70. });