Matheus Henrique
Matheus Henrique

Reputation: 15

Javascript join elements, having problems

I wasted 4 hours searching and trying, but no luck.

if ((navigator.plugins) && (navigator.plugins.length)) {
    for (var xx = 0, l = navigator.plugins.length; xx < l; xx++) {    
        var tn = navigator.plugins[xx].name;    
        if (tn.indexOf("Java") != -1) {
            document.write("Found ");
        } 
        else {
            document.write("Nothing ");
        }    
    }
}

Insteading of writing just "Found" or "Nothing" this script writes the following:

NothingNothingFoundNothing

(there are 4 plugins btw):

How do I fix this so that I only receive one answer? I don't want other way to find if Java is installed, just the above but working.

Edit: Thanks both of you, now it's working.

Upvotes: 0

Views: 63

Answers (2)

Shivam Shah
Shivam Shah

Reputation: 524

you are printing 'Java' and 'Nothing' inside the loop so when it finds java it prints java and when it doesnt find java, it prints nothing. try this:

var javafound = false;
if ((navigator.plugins) && (navigator.plugins.length)) {
  for (var xx = 0, l = navigator.plugins.length; xx < l; xx++) {
    var tn = navigator.plugins[xx].name;

    if (tn.indexOf("Java(TM)") {
      javafound = true;
    } else {
      javafound = false;
    }

  }
}

if (javafound) {
  document.write("Java ");
} else {
  document.write("Nothing ");
}

Upvotes: 0

M. Laing
M. Laing

Reputation: 1617

if ((navigator.plugins) && (navigator.plugins.length)) { 
    var found = false;
    for (var xx = 0, l = navigator.plugins.length; xx < l; xx++) { 

        var tn = navigator.plugins[xx].name; 

        if (tn.indexOf("Java(TM)") { 
           found = true;
        } 

    } 
    if(found) { 
        document.write("Java "); 
    } else { 
        document.write("Nothing "); 
    }
} 

Upvotes: 1

Related Questions