-1

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

3
  • 1
    That code could not possibly produce that output, so what is the actual code and the actual output? Commented Oct 21, 2012 at 15:00
  • oops sorry: first output from IE - edited Commented Oct 21, 2012 at 15:40
  • Spurious ;. That is all. Commented Dec 20, 2024 at 11:09

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Clyde Lobo shows the correct answer. when you encounter JS issues, it's better to debug it with Developer Tool (F12) from your browsers. by setting a break point, it could be easy to find this code bug.
the same after if (browser == 2);
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.