I came across this unique practice in a browser detection script.
if (/MSIE/.test(_3)) {
IE = true;
} else {
if (/AppleWebKit/.test(_3)) {
Safari = true;
} else {
if (/Opera/.test(_3)) {
Opera = true;
} else {
if (/Camino/.test(_3)) {
Camino = true;
} else {
if (/Firefox/.test(_3) || /Netscape/.test(_3) || ) {
Mozilla = true;
}
}
}
}
}
Are there any advantages to using this nested If/Else method?
What if I just changed it to:
if (){
} else if (){
} else if (){
} else if (){
}
Would it run slower or anything?
In this case the
switchstatement could also be used? Something like:I adhere to the previous answer: else if() is equivalent to but more readable then else { if () …}
Anyway, the code you found and presented looks a bit clumsy and not really fast for other reasons than the way
if...elseis used. Concerning the meaning of the code (and aside from the question), a better way to take care of browser differences is by object detection I would say.