Bug Tracker

Opened 13 years ago

Closed 12 years ago

Last modified 8 years ago

#1101 closed enhancement (fixed)

Support of $.browser.version

Reported by: alexo Owned by:
Priority: major Milestone: 1.1.4
Component: core Version: 1.1.3
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by john)

It would be very useful if jQuery could provide a property for retrieving the current browser version... Currently, I cannot find out if, for instance, the browser is msie6 or msie7..

Thank you!

Change History (6)

comment:1 Changed 13 years ago by john

I think I've finally made one that I'm pleased with. It matches the versions of all the browsers that we support, and is very short. Here's a full test:

var browsers = {
"Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)": "7.0",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3": "2.0.0.3",
"Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",
"Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",
"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "419.3",
"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.5"
};

for ( var i in browsers ) {
  console.log( "Expected", browsers[i], i.match(/.+[xiae][/ ]([d.]+)/i)[1] );
}

the addition to jQuery would be:

  version: b.match(/.+[xiae][/ ]([d.]+)/)[1],

comment:2 Changed 13 years ago by john

Resolution: fixed
Status: newclosed

Fixed in SVN rev [1809].

comment:3 in reply to:  2 ; Changed 13 years ago by hobbit

Resolution: fixed
Status: closedreopened

Replying to john:

You detect the browser engine (gecko, webkit) from user agent, it's named to browser type (browser.mozilla, browser.safari), and the browser.version contains the browser build or version number (firefox, safari). If you detect the engine, why not detect the engine's number? Example the browser.mozilla is true under Mozilla, Firefox, Netscape, Flock etc. browsers but the browser.version contains the Firefox version number.

And the script currently halts if not match.

var browsers = {
  //Internet Explorer
  "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",
  "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)": "7.0",

  //Browsers with Gecko engine
  //Mozilla
  "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12",
  //Firefox
  "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3": "1.8.1.3",
  //Netscape
  "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5",
  //Flock
  "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.11) Gecko/20070321 Firefox/1.5.0.11 Flock/0.7.12" : "1.8.0.11",

  //Opera browser
  "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20",
  "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",

  //WebKit engine
  "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9",
  "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8",
  "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8",

  //Other user agent string
  "Other browser's user agent 1.0":null
};

for (var i in browsers){
  v = i.match(/.+(?:rv|it|ra|ie)[/: ]([d.]+)/i);
  version = v? v[1]:null;

  console.log([version==browsers[i], browsers[i], version]);
}

comment:4 in reply to:  3 ; Changed 13 years ago by john

Component: ajaxcore
Resolution: fixed
Status: reopenedclosed

Replying to hobbit: Committed in SVN rev [1872].

comment:5 in reply to:  4 Changed 13 years ago by hobbit

Resolution: fixed
Status: closedreopened

Replying to john:

Replying to hobbit: Committed in SVN rev [1872].

The script throws an error if the match method returns null. The null[1] expression isn't valid. The modification to jQuery would be:

new function() {
  var
    b = navigator.userAgent.toLowerCase(),
    v = b.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/);

  // Figure out what browser is being used
  jQuery.browser = {
    //If not match, the version contains null value.
    version: v? v[1]:v, //v && v[1]
    safari: /webkit/.test(b),
    opera: /opera/.test(b),
    msie: /msie/.test(b) && !/opera/.test(b),
    mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
  };

  // Check to see if the W3C box model is being used
  jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
};

comment:6 Changed 12 years ago by john

Description: modified (diff)
Milestone: 1.1.31.1.4
Resolution: fixed
Status: reopenedclosed
Version: 1.1.21.1.3

Fixed in SVN.

Note: See TracTickets for help on using tickets.