Search code examples
jquerygetscript

Injecting data into jQuery $.getScript call


I need to inject some extra data into $.getScript call, so it'll be availabe in ready handler, how do I do that?

// since it's called in a loop I need pass context into read handler
$.getScript(path, function (e2) {

};

in regular event handlers I can do do by passing the data as a second parameter and get it by e.data.* from within the event handler:

element.on("event", { extra: "data" }, function(e) { console.log(e.data.extra); });

which doesn't seem to work with $.getScript.


Solution

  • The .getSCript() does not provide that functionality, but I think you can make use of closure here

    Ex:

    for(var i = 0; i < x; i++){
        (function(idx){
            $.getScript(path, function (e2) {
                console.log(idx);
            });
        })(i);
    }