Search code examples
javascriptrequirejsrequire

How to append query string arguments to URLs of script/JS files written in Require JS through script/Javascript Code


Looking to query string arguments appended to URLs in Require JS project.


Solution

  • To add a version number in each file URL using requirejs. You can set the value of version in "urlArgs" property of JSON object and pass this object in require.config.

    Please see below code.

    var version = "1.0";
    require.config({
        baseUrl: "/scripts",
        paths: {
            "jquery": "jquery-2.2.1",
            "bootstrap": "jquery.bootstrap"
        },
        urlArgs: "v=" +  version
    });
    
    require(["jquery", "bootstrap"], function (jquery, bootstrap) {
        jquery("#id_div").html("Hello RequireJS!!!");
    });
    

    For more details, you can view documentation of urlArgs.