我试图运行一个脚本来开发一个附加组件。不知何故,第一次运行将返回undefined,只有第二次运行才能获得我想要的结果。我搜索并意识到它可能与吊装有关。有什么想法吗?感谢。
var array1=[];
var array2=[];
var arrayPDS=[];
var arrayPD=[];
var suggPD;
function handleClick(state) {
var suggPD = Request({
url: "http://google.com/complete/search?output=toolbar&q=" + primarydomain,
overrideMimeType: "application/xml; charset=latin1",
onComplete: suggPDparse,
}).get();
function suggPDparse(response) {
var {Cc, Ci} = require("chrome");
var parser = Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);
var xml = parser.parseFromString(response.text, "application/xml");
var pds;
var pd = xml.getElementsByTagName("suggestion");
for (i=1;i<pd.length;i++){
pds = pd[i].getAttribute("data");
array1.push(pds);
arrayPDS = array1[0];
}
function loadXMLDoc(filename, callback){
var req = new XMLHttpRequest();
req.open("GET",filename,true);
req.onreadystatechange = function(){
if(req.readyState === 4){
callback(req.responseXML);
}
}
req.send();
}
loadXMLDoc(self.data.url("pd.xml"), function(xmlDoc){
if (xmlDoc.documentElement.nodeName=="parsererror"){
console.log(xmlDoc.documentElement.childNodes[0].nodeValue);
return(null);
}
var x = xmlDoc.getElementsByTagName("Row");
for (i=0; i<x.length; i++){
var a=x[i];
getV=(a.getElementsByTagName("Field_0")[0].childNodes[0].nodeValue);
array2.push(getV);
}
arrayPD = array2;
});
}
console.log("arrayPD: " + arrayPD);
console.log("arrayPDS: " + arrayPDS);
}
首次点击的结果将返回:
arrayPD:
arrayPDS:
第二次点击的结果将返回:
arrayPD:ABCD
arrayPDS:abcd