Advertisement
livechatinc

Browser detection

Oct 5th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Utils, browser, os, ua;
  2.  
  3. Utils = {
  4.   regexp: {
  5.     UAChrome: /(chrome)[ Chrome\/]([\w.]+)?/,
  6.     UAwebkit: /(safari)[ \/]([\w.]+)/,
  7.     UAopera: /(opera)(?:.*version)?[ \/]([\w.]+)/,
  8.     UAmsie: /(ie) ([\w.]+)/,
  9.     UAmozilla: /(firefox)(?:.*? rv:([\w.]+))?/,
  10.     OSwindows: /(windows)/,
  11.     OSmac: /(macintosh)/,
  12.     OSlinux: /(linux)/,
  13.     OSiOS: /(iphone)/,
  14.     OSiPad: /(ipad)/,
  15.     OSandroid: /(android)/
  16.   },
  17.   uaMatch: function(ua) {
  18.     var match;
  19.     if (typeof ua === 'undefined') {
  20.       return {
  21.         browser: '',
  22.         version: ''
  23.       };
  24.     }
  25.     ua = ua.toLowerCase();
  26.     match = this.regexp.UAChrome.exec(ua) || this.regexp.UAwebkit.exec(ua) || this.regexp.UAopera.exec(ua) || this.regexp.UAmsie.exec(ua) || ua.indexOf("compatible") < 0 && this.regexp.UAmozilla.exec(ua) || [];
  27.     return {
  28.       browser: match[1] || '',
  29.       version: match[2] || ''
  30.     };
  31.   },
  32.   osMatch: function(ua) {
  33.     var match;
  34.     if (typeof ua === 'undefined') {
  35.       return {
  36.         os: ''
  37.       };
  38.     }
  39.     ua = ua.toLowerCase();
  40.     match = this.regexp.OSwindows.exec(ua) || this.regexp.OSmac.exec(ua) || this.regexp.OSandroid.exec(ua) || this.regexp.OSlinux.exec(ua) || this.regexp.OSiOS.exec(ua) || this.regexp.OSiPad.exec(ua) || [];
  41.     return {
  42.       os: match[1]
  43.     };
  44.   }
  45. };
  46.  
  47. browser = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36 LiveChatSmartClient/8.1.12.0";
  48. os = Utils.osMatch(browser);
  49. console.log(os);
  50. ua = Utils.uaMatch(browser);
  51. console.log(ua);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement