Friday, January 19, 2007

Getting jQuery Version

There is a way of getting the jQuery version, but it is not through using jQuery.version as you may expect.

alert("jQuery version is: " + jQuery().jquery);

Here is a basic function that can be used to grab more valuable information from this.

function jQueryVersion()
{
 var ver = (new jQuery()).jquery;
 // if there are two .'s it is a bug fix release
 if(ver.match(/\./g).length == 2)
 {
  return {
   "version" : ver,
   "major" : ver.substr(0, ver.indexOf(".")),
   "minor" : ver.substr(ver.indexOf(".") + 1, ver.lastIndexOf(".") - ver.indexOf(".") - 1),
   "revision" : ver.substr(ver.lastIndexOf(".") + 1)
  }
 }
 // otherwise it is an initial release
 else
 {
  return {
   "version" : ver,
   "major" : ver.substr(0, ver.indexOf(".")),
   "minor" : ver.substr(ver.indexOf(".") + 1),
   "revision" : 0
  }
 }
}

This could be handy when developing plugins, as they could alert the user if they are using a version of jQuery not supported.

jQuery.fn.myplugin = function()
{
  var version = jQueryVersion();
  if(parseInt(version.minor) < 1)
  {
    alert("jQuery version 1.1 or higher is required for myplugin");
    return this;
  }

No comments: