Quick Tip | Uncaught SyntaxError: Unexpected token | Issue with JSON on Chrome.

By | April 27, 2011

I came acrosss the Uncaught SyntaxError: Unexpected token which is just an issue with JSON on Chrome while creating my post on Registration App using HTML 5 Local Storage. When creating the formSave object for the purposes of easy reading I tabbed the declarations so it looked nice and neat as below.

formSave = {
       fname		: $("input[name='firstname']").val(),
       lname 		: $("input[name='lastname']").val(),
       email 		: $("input[name='email']").val(),
       phone		: $("input[name='phone']").val(),
       job		: $("select[name='job']").val(),
       primeuse 	: $("select[name='primeuse']").val(),
       cd		: $("input[name='cd_catalogue']").attr('checked'),
       paper	        : $("input[name='paper_catalogue']").attr('checked'),
       priceDeals	: $("input[name='price_deals']").attr('checked'),
       industryInfo	: $("input[name='industry_info']").attr('checked')
};

Now this shouldn’t affect anything in the browsers and didn’t in Safari and Firefox but Chrome threw an error. Uncaught SyntaxError: Unexpected token. At first I had no idea what was happening. I finally found out that it was the extra tab stops. I removed them all so the code was not as easy to read and it worked on all browsers. It now looks like this:

formSave = {
       fname : $("input[name='firstname']").val(),
       lname : $("input[name='lastname']").val(),
       email : $("input[name='email']").val(),
       phone : $("input[name='phone']").val(),
       job : $("select[name='job']").val(),
       primeuse : $("select[name='primeuse']").val(),
       cd : $("input[name='cd_catalogue']").attr('checked'),
       paper : $("input[name='paper_catalogue']").attr('checked'),
       priceDeals : $("input[name='price_deals']").attr('checked'),
       industryInfo : $("input[name='industry_info']").attr('checked')
};

Very strange. Anyway hope this helps.
P.S. When I wrote this tip I was unable to recreate the error!! So if someone could enlighten me on exactly what I had done that would be great.

Leave a Reply

Your email address will not be published. Required fields are marked *