javascript - debugging chrome extension -


i have code:

...    function init() {       getdata();   }    var txt;   function getdata() {       var xhr = new xmlhttprequest();       xhr.open('get',myurl, true);       xhr.setrequestheader('cache-control', 'no-cache');       xhr.setrequestheader('pragma', 'no-cache');       xhr.onreadystatechange = function() {           if (xhr.readystate == 4) if (xhr.responsetext) {               txt = xhr.responsetext;           }       }        xhr.send();       console.log(txt);   }  ...  <body onload="init();">  ... 

why can't txt value? please help. forgot mention - of happens in background page, dont have other pages @ moment. tried console via extensions overview background page, console doesnt output anything.. :((

the xhr request asynchronous, when log txt console after sending it, request hasn't completed yet txt undefined.

either set run synchronously setting async false in open command, or stick console.log(txt) in function have bound onreadystatechange event already.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -