functions used before defined.
Posted: Fri Jun 25, 2010 9:49 am
I am a complete javascript beginner, but I code quite often in php, and in other software languages. I have a script which was a real mess. I ran it through an online validator and there was a lot wrong with it, no semi colons at all for one thing.
The last thing the validator says is that ''stateChanged' was used before it was defined.' and ''GetXmlHttpObject' was used before it was defined.'
Now I know this means that the functions are being used before they are being defined, that much is completely obvious - but since this code was handed to me and not self written I'm not 100% sure how to tweak it. I know that its to work an optional drop down menu - but its not doing its job properly. Infact its altering a drop down menu that it shouldn't be changing.
Many Thanks,
Aravona
Code: Select all
// JavaScript Documentvar xmlHttp
function showSubjects(links,subjects){
var url="show-subjects.php?sid=" + Math.random() + "&links=" + links + "&subjects=" + subjects;
xmlHttp=GetXmlHttpObject(stateChanged);
xmlHttp.open("GET", url , true);
xmlHttp.send(null);
}
function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("subjectdiv").value="";
document.getElementById("subjectdiv").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject(handler){
var objXmlHttp=null;
if (navigator.userAgent.indexOf("Opera")>=0){
alert("This example doesn't work in Opera");
return;
}
if (navigator.userAgent.indexOf("MSIE")>=0){
var strName="Msxml2.XMLHTTP";
if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
strName="Microsoft.XMLHTTP";
}
try {
objXmlHttp=new ActiveXObject(strName);
objXmlHttp.onreadystatechange=handler ;
return objXmlHttp;
}
catch(e){
alert("Error. Scripting for ActiveX might be disabled");
return;
}
}
if (navigator.userAgent.indexOf("Mozilla")>=0){
objXmlHttp=new XMLHttpRequest();
objXmlHttp.onload=handler;
objXmlHttp.onerror=handler ;
return objXmlHttp;
}
}Now I know this means that the functions are being used before they are being defined, that much is completely obvious - but since this code was handed to me and not self written I'm not 100% sure how to tweak it. I know that its to work an optional drop down menu - but its not doing its job properly. Infact its altering a drop down menu that it shouldn't be changing.
Many Thanks,
Aravona