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
Post a Comment