Weird syntax error for just comment lines - javascript

I am getting some really weird syntax errors [Uncaught SyntaxError: Unexpected identifier] for seemingly normal code.
Getting an error if I write anything after the 1st line in the else block..
if (classlist.length == 0) {
lastClickCatID = -1;
}
else {
lastClickCatID = +classlist.value.split('-')[1];
// **Getting error if I write anything here, even for comments like this**
}
Getting the error if I just use two words in the comment (with a space between them). And of course getting error for any JS statements, even basic console logs.
Also, getting an error for a console.log line in the following code which has been commented out (the 4th line: console.log-"UNDO"):
// Push this data into the undo array
undo.push([lastClickDivID, lastClickCol, lastClickRow, lastClickCatID, nextClickCatID]);
//console.log("UNDO", undo[0][0], undo[0][1], undo[0][2], undo[0][3], undo[0][4]); // *Getting error if I include this line*
console.log(undo.pop());
Getting an error with or without the comment tag. But if I remove the entire line, it works fine.
I have another line:
nextClickCatID = +id2;
Again getting errors for doing a console.log of the variable. But it works fine if I remove the '+' and just use next 'nextClickCatID = id2; '.
Also getting many other weird errors like that within this function (it will get too long if I include them). Any idea why I am getting errors like these for seemingly normal code?

I have solved it. It strangely worked when I removed the space between the () and the curly brackets in the jquery document ready function. Changed from...
$(function() {
to:
$(function(){ // with no space between the parentheses and the curly brackets
I want to add that - it was working fine before with the same structure (with space in between brackets) all this while. But suddenly decided to give an error today in a particular function. If somebody can clarify why this happened, it will still be very relevant.

Related

Why am I getting "Invalid or unexpected token" with a while loop?

I am trying to run a while loop to remove all the elements of a particular class name from the DOM as given in the answer here. For some reason, I am getting an error.
Uncaught SyntaxError: Invalid or unexpected token
The code I am trying to run is lengthy but the basic idea is identical to the above-linked answer.
function resetGame() {
var cardColumns = document.getElementsByClassName('card-column');
while (cardColumns[0]) {
cardColumns[0].parentNode.removeChild(cardColumns[0]);
}
}
When I hover over the error (which shows at the end of the loops curly brace) it shows \u200b in Chrome Inspect.
I have tried changing the class name, and even directly copy-pasting the guy above answer. Same error. When I paste the loop into the console on its own I get the same thing, so my best assumption is I have something incorrect fundamentally with the syntax, not the rest of my code.
Any ideas?

Invalid left-hand side in assignment for no apparent reason

I'm receiving that kind of error, I can't copy the whole code (quite big), but I can find a reason why that happens.
In this portion of code
'... })})(jQInteractives);</script></textarea>')
} else if (ide == "feverchart") {
$("#codeArea").append('<textarea>'+
'<style>.stretch:after {content: ....
I'm using ... just to indicate the code follows there, but it's not a problem of what's around it, even if I move it, the problem always appears to be in the line
$("#codeArea").append('<textarea>'+
It has nothing to do with the '+ because I've tried without it and just continuing the line.
I'm using the exact same line in other parts of the complete code without problems, and I don't see the problem here either.
Any clues?
Per comment, I moved it to a different place, to avoid problems with not properly closing smoething. I have the same problem when I move it here:
if (ide == "blanktemp") {
} else if (ide == "feverchart") {
$("#codeArea").append('<textarea>'+
'<style>.stretch:after {content: ".";display: [... code continues ...]
It's always the same line, no matter where I put it.

Why is a URL in the middle of a function not causing an error?

By mistake, an URL was pasted into a JavaScript snippet. Reduced to a minimum, it looked roughly like this:
function(){
/* a bunch of code */
http://www.stackoverflow.com
/* a bunch of code */
return "it still works";
}
It was overlooked for quite some time, because it did not produce an error. Why is that? Why does this function still run without erroring?
You're defining a label called http. The // in the url comments the rest out.
see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
Before returning a string value of it still works it is creating a label http: note the usage of :. And the remaining part of the code is commented out using the Single Line comment: //
There wasn't any sort of error in your code, because there was no Syntax error in your code. That's why it worked correctly.

Illegal Character error in jQuery - regardless of function content

First of all, I've done my research and I did find a bunch of simialr questions. However, I didn't find an answer that applies to my problem. All the examples I found were related to unescaped characters, single/double quote mishaps and the like. I on the other hand am getting this error on the following function:
$('.seq_input').blur(function(){
//var id = $(this).data('id');
//var index = parseInt($(this).val()),
//element = $("#test-list li").eq(id).remove();
//$("#test-list li").eq(index - 1).before(element); // -1 because users like 1 based indices
alert('what?');
})​​​;
As you see I commented out everything and just left an alert, and I'm still getting the error, pointing to the last line of the function. It couldn't have anything to do with other functions because I just added this one alone at the end of my current Javascript.
Can someone please tell me what's going on here? Why on Earth would a function that just alerts something (or even if it doesn't do anything) give an error?
NOTE: error is shown as soon as the page is loaded
There are invisible characters between the trailing semicolon and the parenthesis. I concatenated your code, put it in a string, and called a non-existent function in order to trigger a error (using this method).
'})​​​;'.l()
>>> TypeError: "})\u200B\u200B\u200B;".l is not a function
$('.seq_input') may used on the other functions, try using new id to do that function.

Unexpected "unexpected end of line" JavaScript lint warning

This is my JavaScript (much stripped down):
function addContent() {
var content = [];
content.append(
makeVal({
value : 1
})
); // Generates lint message
}
Running a lint program over this, I get the message
unexpected end of line; it is ambiguous whether these lines are part of the same statement
on line 7. If I concatenate lines 6 and 7 the message goes away.
Can anyone explain where this ambiguity is? It seems to me that the parenthesis on line 7 is unambiguously closing the call to append().
It looks that way to me, too. Sounds like a bug in the lint program you're using.
You can understand why it would wonder, because the call to makeVal fits the profile of code that's relying on semicolon insertion — unless you look correctly at the wider context and realize it's within argument list for the append call. Seems to me the lint program is not actually parsing the language, just looking for patterns, which is going to mean it's going to have both false positives and false negatives.

Categories

Resources