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?
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.
"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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With