JavaScript 错误(Uncaught SyntaxError: Unexpected end of input)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3983088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript error (Uncaught SyntaxError: Unexpected end of input)
提问by SLaks
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".
“未捕获的语法错误:意外的输入结束”。
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 });
它说错误在最后一行 });
回答by SLaks
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() {
。
回答by falsarella
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("");
回答by Hugo
http://jsbeautifier.org/is helpful to indent your minified JS code.
http://jsbeautifier.org/有助于缩进缩小的 JS 代码。
Also, with Google Chromeyou can use "pretty print". See the example screenshot below showing jquery.min.js
from Stack Overflow nicely indented right from my browser :)
此外,使用谷歌浏览器,您可以使用“漂亮的打印”。请参阅下面的示例屏幕截图,显示jquery.min.js
在我的浏览器中缩进的 Stack Overflow 上:)
回答by hvgotcodes
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"});
});
回答by Y?co
In my case, it ended up being a simple double quoteissue in my bookmarklet, remember only use single quotes on bookmarklets. Just in case this helps someone.
就我而言,它最终成为我的书签中的一个简单的双引号问题,记住只在书签上使用单引号。以防万一这对某人有帮助。
回答by user280109
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 书签时遇到此错误。我无法弄清楚是什么原因造成的。但最终我尝试通过以下网站对书签进行 URL 编码:http: //mrcoles.com/bookmarklet/然后错误消失了,所以一定是 javascript 代码中的某些字符被解释为特殊 URL 的问题控制字符。
回答by Jimmy Obonyo Abor
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);
}
}
});
回答by Sam
In my case, it was caused by a missing (0)
in javascript:void(0)
in an anchor.
对我来说,它是由一个缺失导致(0)
在javascript:void(0)
在一个锚。
回答by justanotherguy
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!
我也得到这个错误指向页面上最后一个脚本块的末尾,只是意识到错误实际上是由于单击了一个带有onclick="pagename"
而不是onclick="window.location='pagename'"
. 它并不总是缺少支架!
回答by Hyman Hales
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 的文件中有一条评论,这对于正在发生的事情来说真的很尴尬 - 尽管当点击VM
预渲染的文件并捕获错误时,你会发现到底是什么错误是,就我而言,它只是取消了我正在使用的一些代码的注释。