«

»

Jul 07

Verify jQuery Version is at Least 1.x.x

Small function to verify if the jQuery library loaded is at least version 1.x.x (where you define the minimum version). Note this function does NOT need jQuery loaded to work, so you can use it to check if jQuery is loaded and version compare if you wish.

if (jqueryGreaterThan('1.4.4'))
{
    // jQuery passed version verification, continue with your script...
}

function jqueryGreaterThan(requiredVersion)
{
    var currentVersion = window.jQuery.fn.jquery;
    var intCurrentVersion = parseInt(currentVersion.replace(/\./g, ""));
    var intRequiredVersion = parseInt(requiredVersion.replace(/\./g, ""));

    if (intCurrentVersion >= intRequiredVersion)
        return true;
    else
        return false;
}

2 comments

  1. Jason Caylor

    This function could be really handy. It’s great for determining if a specific function exists or operates the way you are intending it to.

    I do have a question, and quite possibly a suggestion, however. If you were going to use this function to determine if the current version of jquery that you are using is at least a certain version then why would you need to specify the jquery version as an argument in the function instead of inside the code itself?


    if (jqueryGreaterThan('1.4.4'))
    {
    // jQuery passed version verification, continue with your script...
    }

    function jqueryGreaterThan(requiredVersion)
    {
    var currentVersion = window.jQuery.fn.jquery;
    var intCurrentVersion = parseInt(currentVersion.replace(/\./g, ""));
    var intRequiredVersion = parseInt(requiredVersion.replace(/\./g, ""));

    if (intCurrentVersion >= intRequiredVersion)
    return true;
    else
    return false;
    }

    What do you think?

    1. Ed

      This is very true. Over thinking seems to be an issue when it comes to my functions hah. This way makes more sense and is simpler for the developer. I will edit my current post to reflect these changes. Thanks Jason.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>