I'm trying to pass a variable that looks like 68679786987698_987687697869786 to a function in Javascipt, but I'm getting the error Uncaught SyntaxError: Unexpected token ILLEGAL in Chrome's developer console. It looks like the underscore is the problem, but I need it to stay there. Any suggestions?
Here's the relevant code:
entry += '<span>Like';
function likePost(id) {
alert('like');
}
Use quotes:
entry += '<span>Like';
post.id must be a string if it contains the underscore.
Related
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?
I'm getting the string from controller:
var x = '<?php echo addcslashes($this->x, "'") ?>';
The parsed result is:
var x = '<script>alert(\'x\')</script>';
Error:
Uncaught SyntaxError: Invalid or unexpected token
I tried to assign the string directly from JS and it works.
The alert is coming up because the first character in the alert is a slash (the unexpected token) the second slash escapes the ' so the alert never closes either.
Not really sure what you're trying to achieve honestly with the script tags etc seems as you're already inside a script when it gets called (unless you're printing it to a page, in which case you're better off adding it to an event handler or something.)
I am working on creating this website and my Javascript is having issues.
I'm getting the error: Uncaught SyntaxError: Unexpected identifier
This occurs with one of my lines on my Javascript.
var temp=localStorage.getItem(volunteerOpp);
newObject = JSON.parse(temp);
display newObject[0];
It is showing this error on line 3, I think and I'm unsure what the problem is.
That's the error JSON.parse throws when you don't feed it nice json. Have a close look at volunteerOpp for syntax errors, perhaps a property name not in double quotes.
I'm not sure what you want, but you can try this:
var temp = localStorage.getItem(volunteerOpp),
newObject = JSON.parse(temp);
console.log(temp);
console.log(newObject);
console.log(newObject[0]);
I have a problem parsing a string variable back to an object.I have looked at all other question around this topic but none solve my problem.
if(subMatch.match(/\{.*\}/)){ /// new Object of some sort
var objStr=subMatch.match(/\{.*\}/)[0];
//objStr= JSON.stringify(objStr); // I tried this , no difference
//objStr='"'+objStr+'"'; // Tried this way: unexpected token t
//objStr="'"+objStr+"'"; // Tried this way: unexpected token '
objStr=JSON.parse("'"+objStr+"'"); // puts out unexpected token '
This is the string I am trying it out on:
{"type": "lawnmowing","hours": 10,"rate": 10.5,"permanent": false}
According to JSONLint it is valid. With the extra quotes it looks like:
'{"type": "lawnmowing","hours": 10,"rate": 10.5,"permanent": false}'
I looked at this question,
JSON.Parse,'Uncaught SyntaxError: Unexpected token o
but their variable starts as an object. My objStr has type String, I checked.
I am adding literal single quotes around objStr.
Because objStr is already a String, that should not be a problem right?
I've also tried completely without extra quotes around the variable
How can I correctly JSON.parse a String variable.
I can get it to work with eval, but I rather not use that, because it is user input that I have to put in an object.
Sorry to bother you with a question about this topic again, but I haven't found the solution among the other questions.
Help would be really appreciated!
Thanks
Jenita
As CBroe says, JSON.parse() can parse JSON just fine on its own and whatever you're trying to do here is preventing it from doing that.
It doesn't need your help. Just let it do its job and get rid of all that mess:
var obj = JSON.parse(objStr);
Ok, so I have a template which I am using to print a couple of users to a table.
function PrintUsers(item) {
$.template('userList', '<tr onClick="OnUserPressed(${Identifier})">\
<td>${Firstname}</td>\
<td>${Lastname}</td>\
</tr>');
$.tmpl('userList', item).appendTo("#UserTableContainer");
}
When I press a user I want his/hers unique identifier to be passed to a function called OnUserPressed which I am declaring in the template. The code below is just a test to see if it actually passes the data to the function.
function OnUserPressed(Identifier) {
alert(Identifier);
}
My problems are these: When I press the first value in the table I get "Uncaught SyntaxError: Unexpected token ILLEGAL". When I press any other value in the table I get "Uncaught ReferenceError: xxx is not defined" where xxx is their unique identifier. So it actually retrieves the ID but I still get an error.
Any thoughts?
You probably need to pass the identifier to the OnUserPressed function as a string.
Try wrapping the ${Identifier} template variable with single quotes:
<tr onClick="OnUserPressed('${Identifier}')">
Edit: Responding to comment about single quotes.
Inside your template string you can escape the single quotes by preceeding them with a backslash:
'<tr onClick="OnUserPressed(\'${Identifier}\')">'