How to dynamically load javascript function (with Ajax in Dojo) with arguments -


i implementing require/load function retrieves , executes remote piece of javascript code. works fine, , using workaround give arguments declaring global variables, wanted know best way pass arguments in situation. here function loads remote script

module.require = function (modulename , args){     if (! module.has(modulename)){         dojo.xhrget({             url: 'module.require.php',             handleas: "javascript",             content : {                 module : modulename             },             load: function() {             },             error: function(errormessage) {                 console.log('there error module.require()');                 console.error(errormessage);             }         });     } }; 

my first question context modulename executed in when code stored in modulename.js fetched , dynamically executed? if in scope of function module.require can refer args. if executed in global scope (window) know how declare local variables (anonymous function call (function(){}());) how pass arguments? hope there easy way not wish pass arguments on server. not difficult parse them via php, there code potentially many arguments.

edit: james khoury mentioned how can use f.apply() , f.call() pass arguments, need know context script loaded in on execution. if it's in scope of function, suppose can call f.call(this , args);. , if can this, there anyway call anonymous function? ask because creating modules wrap user code ensure variables user declares remain local, therefore, best ensure wrapper function not global.

thanks

i @ both function .call , function .apply

var obj = { x: 15 }; f.call(obj)  var args = ["hello", 2, {data: "123"}];  f.apply(obj, args); 

(this taken odetocode.com)

then in function f():

function f(message, num, dataobj) {     if (this.x)     {         alert(this.x);         // alternatively alert(x);     }     if(arguments.length > 2)     {         dataobj["message"] = ""         for(var =0; < num; i++)         {              dataobj["message"] += message + "\n";         }         alert(dataobj.message + "\n" + dataobj.data);      }  } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -