222

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
  • 4,640
  • 9
  • 37
  • 59
  • 2
    I know that Chrome's V8, for a `DELETE` on the server if the response was a 200 `success` instead of a [204](http://tools.ietf.org/html/rfc2616#section-10.2.5) `success - no response` I would get this error as well. Just heads up in case anyone is getting this as well. – Eric D. Johnson Oct 20 '14 at 19:00

11 Answers11

433

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() {.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
96

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

JSON.parse(stringifiedJSON);

In other words, what happened was the following:

JSON.parse("");
falsarella
  • 11,640
  • 8
  • 65
  • 104
39

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
  • 11,640
  • 8
  • 65
  • 104
Hugo
  • 1,899
  • 18
  • 22
14

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
  • 7,135
  • 4
  • 17
  • 36
hvgotcodes
  • 109,621
  • 25
  • 195
  • 231
6

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.

Yëco
  • 1,461
  • 15
  • 13
5

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
  • 7,135
  • 4
  • 17
  • 36
Jimmy Obonyo Abor
  • 5,707
  • 9
  • 39
  • 65
5

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

Sebastian Kaczmarek
  • 7,135
  • 4
  • 17
  • 36
Sam
  • 836
  • 7
  • 17
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
  • 11,640
  • 8
  • 65
  • 104
user280109
  • 1,386
  • 2
  • 13
  • 26
  • 2
    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. – mflodin Jun 09 '15 at 09:01
  • Removing comments fixed it for me as well. – Alex Sep 10 '18 at 08:50
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.

Jack Hales
  • 1,469
  • 22
  • 39
  • 1
    I had the same issue, sometimes ftp transfer mess up file's lines, and single line comments may include afterward lines in their line – StudioX Oct 07 '20 at 06:57
0

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!

justanotherguy
  • 416
  • 1
  • 4
  • 14
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.

Petr
  • 1,075
  • 1
  • 13
  • 24