Pls look at the following code:
html…
<pre id='output'></pre>
html…
JS
function log(text) {
var div = document.getElementById('output');
div.innerHTML += text + '\n';
}
function foo() {
var browser=0;
if(navigator.appName.toUpperCase()=="MICROSOFT INTERNET EXPLORER")
browser=1;
else
if(navigator.appName.toUpperCase()=="NETSCAPE")
browser=2;
log ('browser:'+browser);
if (browser==1)
{
log ('IE');
}
if (browser==2);
{
log ('Chrome');
}
if (browser==0);
{
log ('Could not determine broweser type');
return;
}
}
When I run this from IE the output is:
browser:1 ie not supported Chrome extension will be loaded Could not determine broweser type
When I run it from Chrome the output is:
browser:2
Chrome extension will be loaded
Could not determine broweser type
How can it be that browser has more than one value?
10xs,
Nir
You have a very beginners mistake in your code
The
;at the end of the ifif (browser==0);causes your if condition to end and the rest is a normal code block which gets executed every time no matter the value ofbrowser