Web Design

Javascript 判斷瀏覽器和版本
  • 1# JDP
  • 763312013-6-21 15:18:34
2013/11/22 新增支援IE11

  1. var Sys = {};
  2. var ua = navigator.userAgent.toLowerCase();


  3. //Mozilla/5.0 (Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; rv:11.0) like GeckoIE: 11 (32bit)
  4. if (!!navigator.userAgent.match(/Trident\/7\./))
  5. {
  6.         //document.write(!!navigator.userAgent.match(/Trident\/7\./));
  7.         var isIE11 = true;
  8. }
  9. //IE: mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0; .net clr 2.0.50727; infopath.2; .net clr 3.0.4506.2152; .net clr 3.5.30729)IE: 8.0
  10. else if (window.ActiveXObject){
  11.         Sys.ie = ua.match(/msie ([\d.]+)/)[1];  //正則匹配 "msie 8.0"
  12. }
  13. //else if (document.getBoxObjectFor){  // ff升級以後就這個屬性就變成undefined
  14. //firefox:  mozilla/5.0 (windows nt 5.1; rv:17.0) gecko/20100101 firefox/17.0
  15. else if(ua.indexOf("firefox")!=-1){
  16.         Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]; //正則匹配"firefox/17.0"
  17. }
  18. //Chrome有一個MessageEvent函數,但Firefox也有。不過,好在Chrome並沒有Firefox的getBoxObjectFor函數,根據這個條件還是可以準確判斷出Chrome瀏覽器的
  19. //chrome: mozilla/5.0 (windows nt 5.1) applewebkit/537.11 (khtml, like gecko) chrome/23.0.1271.95 safari/537.11Chrome: 23.0.1271.95
  20. else if (window.MessageEvent && !document.getBoxObjectFor){
  21.         Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1]; //正則匹配"chrome/23.0.1271.95"
  22. }
  23. //Opera提供了專門的瀏覽器標誌,就是window.opera屬性
  24. else if (window.opera){
  25.         Sys.opera = ua.match(/opera.([\d.]+)/)[1];
  26. }
  27. //Safari瀏覽器中有一個其他瀏覽器沒有的openDatabase函數,可做為判斷Safari的標誌
  28. else if (window.openDatabase){
  29.         Sys.safari = ua.match(/version\/([\d.]+)/)[1];
  30. }
  31. //以下進行測試
  32. if(Sys.ie) document.write('IE: '+Sys.ie);
  33. if(isIE11) document.write('IE: 11');
  34. if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
  35. if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
  36. if(Sys.opera) document.write('Opera: '+Sys.opera);
  37. if(Sys.safari) document.write('Safari: '+Sys.safari);
複製代碼
Reference:
http://lyf-justdo.rhcloud.com/20 ... rowser-and-version/
http://www.monmonkey.com/javascript/liulanqi2.html
倒序瀏覽 看全部 全部回復1
  • 2# JDP
  • 2013-11-22 13:15:58

Javascript 判斷是否為IE11

IE11 的 UserAgent 變成這樣
Mozilla/5.0 (Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; rv:11.0)

判斷方式要變成
var isIE11 = !!navigator.userAgent.match(/Trident\/7\./);

Reference: http://stackoverflow.com/questio ... -11-with-javascript
回復 收藏 轉播 分享 淘帖