JavaScript error (Uncaught SyntaxError: Unexpected end of input)

JavaScript error (Uncaught SyntaxError: Unexpected end of input)

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:

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

Solutions/Answers:

Solution 1:

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

Solution 2:

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

JSON.parse(stringifiedJSON);

In other words, what happened was the following:

JSON.parse("");

Solution 3:

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

Solution 4:

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"});
        });

Solution 5:

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.

Related:  Why is it called XMLHttpRequest?

Solution 6:

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.