0

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

4

2 回答 2

5

You have a very beginners mistake in your code

The ; at the end of the if if (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 of browser

于 2012-10-21T14:59:46.403 回答
0

You should not put ; after an if statement: if (browser==2);

于 2012-10-21T15:00:16.153 回答