Reputation: 53
what's the difference between this 2 code?
ONE: if xmlhttp.readystate==4, then if xmlHttp.status==200, then execute code
function handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById('underInput').innerHTML = message;
setTimeout('process()', 1000);
}else{
alert('Something went wrong!');
}
}
}
TWO: if xmlHttp.readtState==4 and xmlHttp.Status==200 then execute code
function handleSxerverResponse(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documnetElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById('underInput').innerHTML = message;
setTimeout('process()', 1000);
}else{
alert('Something went wrong!');
}
}
They both look same to me, but only the first one did what I want, instead the second one keep showing the alert message.
Upvotes: 1
Views: 132
Reputation: 19066
The only difference is that in the first one, if readyState
is not 4, you wont see the alert.
If you converted the first one to this:
function handleServerResponse() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById('underInput').innerHTML = message;
setTimeout('process()', 1000);
} else {
alert('Something went wrong!');
}
} else {
alert('Something went wrong!'); //added this
}
}
They would functionally be the same thing. So with the first one you could easily customize the alert based on what 'went wrong' if you wanted.
Upvotes: 0
Reputation: 602
Suppose the first part is false.
In the case of then if
you will never enter the block, so you will never see the alert in the second if statement. If you use &&
you will enter the else
block if either is false.
Upvotes: 0
Reputation: 382150
Before the ready state is 4, it is 3. And then
in the first case the outside test prevents the alert
in the second case, the else
clause applies so the alert
is executed.
Upvotes: 1