262

I have some JavaScript code that works in FireFox but not in Chrome or IE.

In the Chrome JS Console I get the follow error:

"Uncaught SyntaxError: Unexpected end of input".

The JavaScript code I am using is:

<script>
 $(function() {
 $("#mewlyDiagnosed").hover(function() {
 $("#mewlyDiagnosed").animate({'height': '237px', 'top': "-75px"});
 }, function() {
 $("#mewlyDiagnosed").animate({'height': '162px', 'top': "0px"});
 });
</script>

It says the error is on the last line which is });

Phillip
5,79812 gold badges47 silver badges64 bronze badges
asked Oct 20, 2010 at 23:24
1
  • 3
    I know that Chrome's V8, for a DELETE on the server if the response was a 200 success instead of a 204 success - no response I would get this error as well. Just heads up in case anyone is getting this as well. Commented Oct 20, 2014 at 19:00

12 Answers 12

479

Add a second });.

When properly indented, your code reads

$(function() {
 $("#mewlyDiagnosed").hover(function() {
 $("#mewlyDiagnosed").animate({'height': '237px', 'top': "-75px"});
 }, function() {
 $("#mewlyDiagnosed").animate({'height': '162px', 'top': "0px"});
 });
MISSING!

You never closed the outer $(function() {.

answered Oct 20, 2010 at 23:27
Sign up to request clarification or add additional context in comments.

5 Comments

The lesson is, always indent your code (and indent it correctly).
The lesson is, always use good editor with good plugins, which can detect parentheses pairing, highlight it or even repair problems.
@HeyWatchThis You might try Syntastic for Vim.
I use neomake for vim/neovim, configured to run jshint every time I save a JavaScript file. If I have syntax errors, I'll see warning/error symbols in vim's/neovim's gutter. When I put my cursor on that line, the vim/neovim command line will show a message saying what the error is.
Wonder how it could have possibly "worked in Firefox"... :-/
102

In my case, I was trying to parse an empty JSON:

JSON.parse(stringifiedJSON);

In other words, what happened was the following:

JSON.parse("");
answered May 29, 2014 at 17:31

1 Comment

This, unfortunatly, includes $.parseJSON of jquery 1.11.2
40

http://jsbeautifier.org/ is helpful to indent your minified JS code.

Also, with Google Chrome you can use "pretty print". See the example screenshot below showing jquery.min.js from Stack Overflow nicely indented right from my browser :)

enter image description here

falsarella
12.5k10 gold badges75 silver badges119 bronze badges
answered Jun 30, 2013 at 8:13

Comments

16

Formatting your code a bit, you have only closed the inner hover function. You have not closed the outer parts, marked below:

$(// missing closing)
 function() { // missing closing }
 $("#mewlyDiagnosed").hover(
 function() {
 $("#mewlyDiagnosed").animate({'height': '237px', 'top': "-75px"});
 }, 
 function() {
 $("#mewlyDiagnosed").animate({'height': '162px', 'top': "0px"});
 });
Sebastian Kaczmarek
8,5254 gold badges25 silver badges44 bronze badges
answered Oct 20, 2010 at 23:30

Comments

12

In my case, it was caused by a missing (0) in javascript:void(0) in an anchor.

Sebastian Kaczmarek
8,5254 gold badges25 silver badges44 bronze badges
answered Mar 4, 2018 at 20:53

1 Comment

Same issue: but, I had javascript:void(0 missing a )
7

In my case, it ended up being a simple double quote issue in my bookmarklet, remember only use single quotes on bookmarklets. Just in case this helps someone.

answered Sep 20, 2012 at 19:14

1 Comment

Thank you so much, I was totally confused about why my bookmarklet is not working. Now it seems to work! Any specific reason, why double quotes aren't accepted in bookmarklets?
6

This error is mainly caused by empty returned ajax calls, when trying to parse an empty JSON.

To solve this test if the returned data is empty

$.ajax({
 url: url,
 type: "get",
 dataType: "json",
 success: function (response) {
 if(response.data.length == 0){
 // EMPTY
 }else{
 var obj =jQuery.parseJSON(response.data);
 console.log(obj);
 }
 }
});
Sebastian Kaczmarek
8,5254 gold badges25 silver badges44 bronze badges
answered Dec 30, 2015 at 17:41

Comments

3

I got this error when I was trying to write a javascript bookmarklet. I couldn't figure out what was causing it. But eventually I tried URL encoding the bookmarklet, via the following website: http://mrcoles.com/bookmarklet/ and then the error went away, so it must have been a problem with certain characters in the javascript code being interpreted as special URL control characters.

falsarella
12.5k10 gold badges75 silver badges119 bronze badges
answered Oct 27, 2014 at 17:20

2 Comments

I had a // comment in my js, which commented out a bit more than I wanted since pasting it into the url of the bookmarklet made the whole code a one-liner and the // was in the middle of the code.
Removing comments fixed it for me as well.
1

I got this since I had a comment in a file I was adding to my JS, really awkward reason to what was going on - though when clicking on the VM file that's pre-rendered and catches the error, you'll find out what exactly the error was, in my case it was simply uncommenting some code I was using.

answered Feb 20, 2017 at 5:48

1 Comment

I had the same issue, sometimes ftp transfer mess up file's lines, and single line comments may include afterward lines in their line
1

I also got this error pointing to the end of the last script block on a page, only to realize the error was actually from clicking on an element with a onclick="pagename" instead of onclick="window.location='pagename'". It's not always a missing bracket!

answered Jul 9, 2019 at 14:09

Comments

0

I think it could be almost any javascript error/typing error in your application. I tried to delete one file content after another and finally found the typing error.

answered Oct 13, 2020 at 12:49

1 Comment

I am getting this error on the second and sunsequent calls. The first call works fine but thereafter i get the error and it points to line one of the html. Unhelpful. I wrote a program to check all brces, brackets and quotes and all are balanced. The code is quite complex maybe - and there is a lot of it. Any other ideas please?
-1

I got this error because of this line: <button type="button" id="button" onclick="Continue(); document.body.style.cursor = "wait"; this.disabled = true;"> Continue </button>

Note the double-quotes around wait. Changing the double-quotes around wait to single-quotes fixed it.

EDIT: Improved explanation. Changing the double quotes around wait to single quotes was necessary because the entire value for onclick was enclosed within double quotes. Leaving the double quotes around wait and changing the double quotes around the entire value for onclick to single quotes would also fix the issue.

answered Nov 11, 2024 at 23:51

Comments