JavaScript错误(未捕获的SyntaxError:输入的意外结束)

时间:2022-03-03 22:47:11

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

我有一些JavaScript代码可以在FireFox中运行,但不是在Chrome或IE中。

In the Chrome JS Console I get the follow error:

在Chrome JS控制台,我得到了以下错误:

"Uncaught SyntaxError: Unexpected end of input".

未捕获的SyntaxError:输入的意外结束。

The JavaScript code I am using is:

我使用的JavaScript代码是:

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

它说错误在最后一行是});

9 个解决方案

#1


345  

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

您从未关闭外部$(function(){。

#2


85  

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

在我的例子中,我试图解析一个空JSON:

JSON.parse(stringifiedJSON);

In other words, what happened was the following:

换句话说,所发生的情况如下:

JSON.parse("");

#3


35  

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

http://jsbeautifier.org/有助于缩进您的小型JS代码。

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 :)

另外,使用谷歌Chrome你可以使用“漂亮的打印”。查看下面的示例屏幕截图,显示了jquery.min。来自堆栈溢出的js很好地缩进了我的浏览器:)

JavaScript错误(未捕获的SyntaxError:输入的意外结束)

#4


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

#5


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.

在我的例子中,它最终是我的bookmarklet中一个简单的双引号问题,记住,在bookmarklet中只使用单引号。以防万一。

#6


2  

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.

我在编写javascript小书签时犯了这个错误。我不知道是什么引起的。但最终我尝试了通过以下网站对bookmarklet进行URL编码:http://mrcoles.com/bookmarklet/,然后错误消失了,因此javascript代码中的某些字符被解释为特殊的URL控制字符肯定是一个问题。

#7


2  

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

当尝试解析空Json时,这个错误主要是由空返回的ajax调用引起的。

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

#8


0  

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.

我收到了因为我有一个评论在一个文件中我增加了JS,真正尴尬的理由到底发生了什么——尽管当点击虚拟机文件预渲染和捕获的错误,你会发现这个错误到底是什么样子,在我的例子中我用它只是取消一些代码。

#9


0  

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

在我的例子中,它是由锚点中缺少的(0):void(0)引起的。

#1


345  

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

您从未关闭外部$(function(){。

#2


85  

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

在我的例子中,我试图解析一个空JSON:

JSON.parse(stringifiedJSON);

In other words, what happened was the following:

换句话说,所发生的情况如下:

JSON.parse("");

#3


35  

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

http://jsbeautifier.org/有助于缩进您的小型JS代码。

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 :)

另外,使用谷歌Chrome你可以使用“漂亮的打印”。查看下面的示例屏幕截图,显示了jquery.min。来自堆栈溢出的js很好地缩进了我的浏览器:)

JavaScript错误(未捕获的SyntaxError:输入的意外结束)

#4


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

#5


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.

在我的例子中,它最终是我的bookmarklet中一个简单的双引号问题,记住,在bookmarklet中只使用单引号。以防万一。

#6


2  

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.

我在编写javascript小书签时犯了这个错误。我不知道是什么引起的。但最终我尝试了通过以下网站对bookmarklet进行URL编码:http://mrcoles.com/bookmarklet/,然后错误消失了,因此javascript代码中的某些字符被解释为特殊的URL控制字符肯定是一个问题。

#7


2  

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

当尝试解析空Json时,这个错误主要是由空返回的ajax调用引起的。

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

#8


0  

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.

我收到了因为我有一个评论在一个文件中我增加了JS,真正尴尬的理由到底发生了什么——尽管当点击虚拟机文件预渲染和捕获的错误,你会发现这个错误到底是什么样子,在我的例子中我用它只是取消一些代码。

#9


0  

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

在我的例子中,它是由锚点中缺少的(0):void(0)引起的。