JavaScript error (Uncaught SyntaxError: Unexpected end of input)

JavascriptJquerySyntaxSyntax ErrorIndentation

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

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 - Javascript

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 - Javascript

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

Solution 6 - Javascript

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.

Solution 7 - Javascript

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

Solution 8 - Javascript

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.

Solution 9 - Javascript

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.

Solution 10 - Javascript

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.

Solution 11 - Javascript

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser482375View Question on Stackoverflow
Solution 1 - JavascriptSLaksView Answer on Stackoverflow
Solution 2 - JavascriptfalsarellaView Answer on Stackoverflow
Solution 3 - JavascriptHugoView Answer on Stackoverflow
Solution 4 - JavascripthvgotcodesView Answer on Stackoverflow
Solution 5 - JavascriptSamView Answer on Stackoverflow
Solution 6 - JavascriptYëcoView Answer on Stackoverflow
Solution 7 - JavascriptJimmy Obonyo AborView Answer on Stackoverflow
Solution 8 - Javascriptuser280109View Answer on Stackoverflow
Solution 9 - JavascriptJack HalesView Answer on Stackoverflow
Solution 10 - JavascriptPetrView Answer on Stackoverflow
Solution 11 - JavascriptjustanotherguyView Answer on Stackoverflow