Passing global Javascript variables through callback functions to other JS files -


so have interesting problem. declare global variable before referencing other javascript files, variable not seem carry through. maybe missing step somewhere?

the variable string contents of uploaded file so:

function gettext(s){ //creates global variable can hold file         window.text = s;         console.log(window.text);   };        function handlefileselect(event) {     var files = event.target.files; // file uploaded       var reader = new filereader();                       reader.onloadend = function(event){                     gettext(event.target.result); //pulling out content of file         loadscript('total.js',function(){ //starts app code             window.text         });      };           reader.readastext(files[0],"utf-8");//required read text         };              try{         document.getelementbyid('files').addeventlistener('change',          handlefileselect, false); //listens uploaded file   }       catch(e){         alert(e);   }; 

so once user uploads file, contents copied window.text so. contents logged ensure caught right thing. loadscript allows rest of app code start running. needed program wait user specify file before continue, , had weird quirks angularjs, running in manner workaround think of.

the contents of loadscript:

function loadscript(url, callback){  var script = document.createelement("script") script.type = "text/javascript";  script.onload = function(){     callback(); };  script.src = url; document.getelementsbytagname("head")[0].appendchild(script); 

};

total.js begins line of code, leads output:

var csv=window.text; 

everything works. output looks should...save it's empty. meaning window.text isn't pulling through correctly.

is due callback it's not making way through? how can solve this?

edit: function callback isn't running @ all, i've figured out, it's not issue of passing variable through. phpglue's input still valid. thanks.

instead of window.text, declare variable with, recommend, or without var keyword, above of code, like:

var windowtext; function gettext(s){     windowtext = s;     console.log(windowtext); } 

in javascript variables automatically carry down, if written above other code, unless scoped inside function specifically. long don't use var keyword in function code, can reuse windowtext.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -