# Javascript error in Firefox and Opera



## rrrperson (Apr 27, 2006)

Hello all. I'm having some trouble with a simple javascript of mine. Firefox and Opera are giving me a syntax error that I cannot find myself. Also, neither IE7 nor the ever-so-useful Windows Based Script Host can find any errors. At all. I am at a loss over what to do.

The code is relatively short so here it is:

```
var x=navigator;
var brsr="Unknown";
var bver="Unknown";
var ua=x.userAgent;

if ((x.appMinorVersion == null) || (x.appMinorVersion != 0)){
if (ua.indexOf("Opera") != -1){
brsr="Opera";
bver=ua.substr(ua.indexOf("Opera")+6,4)};
else {
brsr="an unknown browser,";
bver="unknown"}};
else if ((ua.indexOf("MSIE") != -1) && (ua.indexOf("Opera") != -1)){
brsr="Opera";
bver=ua.substr(ua.indexOf("Opera")+6,4)};
else if ((ua.indexOf("MSIE") != -1) && (ua.indexOf("America") != -1)){
brsr="AOL Browser";
bver=ua.substr(ua.indexOf("America")+23,3)};
else if ((ua.indexOf("MSIE") != -1) && (ua.indexOf("Avant") != -1)){
brsr="Avant";
bver=ua.substr(ua.indexOf("MSIE")+5,3)};
else if ((ua.indexOf("MSIE") != -1) && (x.appMinorVersion == 0)){
brsr="MS Internet Explorer";
bver=ua.substr(ua.indexOf("MSIE")+5,3)};
else if (ua.indexOf("Macintosh") != -1){
if (ua.indexOf("Camino") != -1){
brsr="Camino";
bver=ua.substr(ua.indexOf("Camino")+7)};
else if (ua.indexOf("Safari") != -1){
brsr="Safari";
bver=ua.substr(ua.indexOf("Safari")+7)};
else {
brsr="an unknown Mac browser,";
bver="unknown"}};
else if (ua.indexOf("Firefox") != null){
brsr="Firefox";
bver=ua.substr(ua.indexOf("Firefox")+8)}
else {
brsr="an unknown browser,";
bver="unknown"};

document.write("You are using " + brsr + " version " + bver + "<br>");
document.write(ua);
```
If someone can look this over and give me their opinion, I would much appreciate it.


----------



## tomdkat (May 6, 2006)

If you can post the HTML that uses this JavaScript, that will make it easier for us to debug. At least a sample HTML page that uses this JavaScript would be helpful.

EDIT: Also, what error are you getting in Firefox and Opera?

Peace...


----------



## rrrperson (Apr 27, 2006)

HTML page is attached. Just change it to .html. And I doubt the problem is in the page, as it validates with the W3C. 

Firefox gives me this exact message: "Syntax error, line 22"
That's all the information it gives me (other than the contents on that line: "else {")

And Opera doesn't show any errors on its console, but it just displays a blank page. No text at all. 

Thanks for any help you can give me.


----------



## tomdkat (May 6, 2006)

rrrperson said:


> HTML page is attached. Just change it to .html. And I doubt the problem is in the page, as it validates with the W3C.


Thanks for the test case. I wasn't thinking anything was wrong with your HTML at all. I wanted the HTML to provide the context in which the JavaScript is being used. 



> Firefox gives me this exact message: "Syntax error, line 22"
> That's all the information it gives me (other than the contents on that line: "else {")


Did you look at line 22 to see what's going on?



> And Opera doesn't show any errors on its console, but it just displays a blank page. No text at all.


Hmmmm, that's strange. The Opera Error Console displayed an error similar to Firefox for me. I'm using Opera 9.5b2. Attached is a screenshot

In any event, the semi-colon after the curly brace at the end of the "if" in the "if/else" block looks suspicious:


```
if ((x.appMinorVersion == null) || (x.appMinorVersion != 0)){
if (ua.indexOf("Opera") != -1){
brsr="Opera";
bver=ua.substr(ua.indexOf("Opera")+6,4)}[b];[/b]   // <=== look here!
else {
brsr="an unknown browser,";
```
 I don't think you want the semi-colon after the curly brace. That terminates the "if/else" block prematurely and leaves the "else" dangling, so to speak. Try removing the semi-colons after the curly braces marking the end of the "if" block. When I removed the semi-colon I indicated above, the error moved to the next occurrence of a semi-colon prematurely terminating an "else" block.

Peace...


----------



## MMJ (Oct 15, 2006)

Off topic, but nice code.


----------



## rrrperson (Apr 27, 2006)

The semicolon removal fixed it! Thank you very much for your help, tomdkat. 
And now that it works, I have discovered that it won't identify Firefox correctly. Oh well. Work in progress. 

@MMJ: Thanks, and I'm assuming you mean the JS.


----------

