Skip to content

Instantly share code, notes, and snippets.

@epicserve
Created March 17, 2010 16:06
Show Gist options
  • Save epicserve/335390 to your computer and use it in GitHub Desktop.
Save epicserve/335390 to your computer and use it in GitHub Desktop.
Javascript required version check
/*
Test if the current version meets the required version number.
Examples:
Basic Usage
if (is_required_version("1.2.6", "1.1.3") === false) {
alert("This script requires version 1.2.6 and the current script's version is 1.1.3");
}
Test if the currently loaded version of jQuery meets a jQuery plugin requirements.
if (typeof jQuery !== "function") {
throw new Error("jQuery isn't loaded, please load jQuery 1.2.6 or greater in order to use this plugin.");
}
if (is_required_version("1.2.6", $.fn.jquery) === false) {
throw new Error("This plugin requires jQuery 1.2.6. and the current version of jQuery is "+ $.fn.jquery +".");
}
*/
function is_required_version(required, current) {
var cv_arr = current.split(".");
var rv_arr = required.split(".");
for (i=0; i <= cv_arr.length-1; i++) {
cv_num = (typeof cv_arr[i] !== 'undefined' && isNaN(cv_arr[i]) === false) ? parseInt(cv_arr[i], 10) : NaN;
rv_num = (typeof rv_arr[i] !== 'undefined' && isNaN(rv_arr[i]) === false) ? parseInt(rv_arr[i], 10) : NaN;
if (cv_num > rv_num) {
return true;
} else if ((cv_num === rv_num) && typeof rv_arr[i+1] === "undefined") {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment