Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a script called "utils.js" that contains multiple functions that can be reused cross multiple files. So how can i convert this function in a way to work in multiple files when is called?

Example:

JavaScript
function myMultiscriptsGetter(pScript){
// do the same logic of $.getMultiScripts
//Note: this function will be in my "utils.js" file

}


Then in any other script i just need to include the "utils.js" and call the function, like this :
JavaScript
$.myMultiscriptsGetter(pScript).done(function() {
            // all scripts loaded !
        });

Note: I tried this but it doesn't work. I dont have a lot experience with jquery

What I have tried:

Currently, i have this function:

JavaScript
$.getMultiScripts = function(arr, path) {
            var _arr = $.map(arr, function(scr) {
                return $.getScript( (path||"") + scr );
            });

            _arr.push($.Deferred(function( deferred ){
                $( deferred.resolve );
            }));

            return $.when.apply($, _arr);
        }


Then, is called:

JavaScript
$.getMultiScripts(pScript).done(function() {
// all scripts loaded !
});
Posted
Updated 8-Jul-18 21:11pm

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900