Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript unexpected identifier

Tags:

I am trying to compress my JavaScript code to get less traffic on my site. It has been working fine, but now I came across an error I can't resolve.

I turned my ajax function into one line:

function(){if(xmlhttp.readyState==4&&xmlhttp.status==200){document.getElementById("content").innerHTML=xmlhttp.responseText;}}xmlhttp.open("GET","data/"+id+".html",true);xmlhttp.send();}

But the chrome console tells me there is an unexpected identifier on this line. Firefox says there is an semicolon missing on this line.

I have been trying to figure out what is wrong, but I can't find the error, can someone help me with this?

like image 824
Jerodev Avatar asked Jun 22 '11 12:06

Jerodev


People also ask

How do I fix unexpected token error?

As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.

What does syntax error unexpected identifier mean?

"Unexpected identifier" means that you have a variable you're trying to reference that hasn't been declared. Make sure you pass all the variables you're trying to use into your template.

What is unexpected end of input in JavaScript?

The "Uncaught SyntaxError Unexpected end of input" error occurs for 3 main reasons: Forgetting a closing parenthesis, bracket or quote. Trying to parse an empty response with JSON.

What is JavaScript token?

Tokens: These are words or symbols used by code to specify the application's logic. These include +, -, ?, if, else, and var. These are reserved by the JavaScript engine and cannot be misused. They also cannot be used as part of variable names.


1 Answers

Yes, you have a } too many. Anyway, compressing yourself tends to result in errors.

function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("content").innerHTML = xmlhttp.responseText;
    }
} // <-- end function?
xmlhttp.open("GET", "data/" + id + ".html", true);
xmlhttp.send();
}

Use Closure Compiler instead.

like image 123
pimvdb Avatar answered Oct 06 '22 08:10

pimvdb