Javascript:函数体后缺少语法错误}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11945216/
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: Syntax error missing } after function body
提问by Lee
Ok, so you know the error, but why on earth am I getting it? I get no errors at all when this is run locally but when I uploaded my project I got this annoying syntax error. I've checked firebug error console, which doesn't help because it put all my source on the same line, and I've parsed it through Lint which didn't seem to find the problem either - I just ended up formatting my braces differently in a way that I hate; on the same line as the statement, bleugh.
好的,所以你知道错误,但我到底为什么会得到它?当它在本地运行时,我根本没有收到任何错误,但是当我上传我的项目时,我遇到了这个烦人的语法错误。我检查了萤火虫错误控制台,这没有帮助,因为它将我的所有源代码放在同一行上,并且我已经通过 Lint 对其进行了解析,这似乎也没有发现问题 - 我只是最终格式化了我的大括号以我讨厌的方式不同;与声明在同一行,bleugh。
function ToServer(cmd, data) {
var xmlObj = new XMLHttpRequest();
xmlObj.open('POST', 'handler.php', true);
xmlObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlObj.send(cmd + data);
xmlObj.onreadystatechange = function() {
if(xmlObj.readyState === 4 && xmlObj.status === 200) {
if(cmd == 'cmd=push') {
document.getElementById('pushResponse').innerHTML = xmlObj.responseText;
}
if(cmd == 'cmd=pop') {
document.getElementById('messages').innerHTML += xmlObj.responseText;
}
if(cmd == 'cmd=login') {
if(xmlObj.responseText == 'OK') {
self.location = 'index.php';
}
else {
document.getElementById('response').innerHTML = xmlObj.responseText;
}
}
}
}
}
function Login() {
// Grab username and password for login
var uName = document.getElementById('uNameBox').value;
var pWord = document.getElementById('pWordBox').value;
ToServer('cmd=login', '&uName=' + uName + '&pWord=' + pWord);
}
// Start checking of messages every second
window.onload = function() {
if(getUrlVars()['to'] != null) {
setInterval(GetMessages(), 1000);
}
}
function Chat() {
// Get username from recipient box
var user = document.getElementById('recipient').value;
self.location = 'index.php?to=' + user;
}
function SendMessage() {
// Grab message from text box
var from = readCookie('privateChat');
var to = getUrlVars()['to'];
var msg = document.getElementById('msgBox').value;
ToServer('cmd=push','&from=' + from + '&to=' + to + '&msg=' + msg);
// Reset the input box
document.getElementById('msgBox').value = "";
}
function GetMessages() {
// Grab account hash from auth cookie
var aHash = readCookie('privateChat');
var to = getUrlVars()['to'];
ToServer('cmd=pop','&account=' + aHash + '&to=' + to);
var textArea = document.getElementById('messages');
textArea.scrollTop = textArea.scrollHeight;
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
One gold internet medal to whoever can solve this.
谁能解决这个问题,就可以获得一枚互联网金牌。
Cheers.
干杯。
回答by xdazz
The problem is your script in your server is in one line, and you have comments in it. the code after //
will be considered as comment. That's the reason.
问题是您服务器中的脚本在一行中,并且您在其中添加了注释。后面的代码//
将被视为注释。这就是原因。
function ToServer(cmd, data) { var xmlObj = new XMLHttpRequest(); xmlObj.open('POST', 'handler.php', true); xmlObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlObj.send(cmd + data); xmlObj.onreadystatechange = function() { if(xmlObj.readyState === 4 && xmlObj.status === 200) { if(cmd == 'cmd=push') { document.getElementById('pushResponse').innerHTML = xmlObj.responseText; } if(cmd == 'cmd=pop') { document.getElementById('messages').innerHTML += xmlObj.responseText; } if(cmd == 'cmd=login') { if(xmlObj.responseText == 'OK') { self.location = 'index.php'; } else { document.getElementById('response').innerHTML = xmlObj.responseText; } } } };}function Login() { // Grab username and password for login var uName = document.getElementById('uNameBox').value; var pWord = document.getElementById('pWordBox').value; ToServer('cmd=login', '&uName=' + uName + '&pWord=' + pWord);}// Start checking of messages every secondwindow.onload = function() { if(getUrlVars()['to'] != null) { setInterval(GetMessages(), 1000); }}function Chat() { // Get username from recipient box var user = document.getElementById('recipient').value; self.location = 'index.php?to=' + user;}function SendMessage() { // Grab message from text box var from = readCookie('privateChat'); var to = getUrlVars()['to']; var msg = document.getElementById('msgBox').value; ToServer('cmd=push','&from=' + from + '&to=' + to + '&msg=' + msg); // Reset the input box document.getElementById('msgBox').value = "";}function GetMessages() { // Grab account hash from auth cookie var aHash = readCookie('privateChat'); var to = getUrlVars()['to']; ToServer('cmd=pop','&account=' + aHash + '&to=' + to); var textArea = document.getElementById('messages'); textArea.scrollTop = textArea.scrollHeight;}function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null;}function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; }); return vars;}
回答by Jaime Torres
You're missing a semi-colon:
你缺少一个分号:
function ToServer(cmd, data) {
var xmlObj = new XMLHttpRequest();
xmlObj.open('POST', 'handler.php', true);
xmlObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlObj.send(cmd + data);
xmlObj.onreadystatechange = function() {
if(xmlObj.readyState === 4 && xmlObj.status === 200) {
if(cmd == 'cmd=push') {
document.getElementById('pushResponse').innerHTML = xmlObj.responseText;
}
if(cmd == 'cmd=pop') {
document.getElementById('messages').innerHTML += xmlObj.responseText;
}
if(cmd == 'cmd=login') {
if(xmlObj.responseText == 'OK') {
self.location = 'index.php';
}
else {
document.getElementById('response').innerHTML = xmlObj.responseText;
}
}
}
}; //<-- Love the semi
}
Additional missing semi-colon:
其他缺少的分号:
// Start checking of messages every second
window.onload = function() {
if (getUrlVars()['to'] != null) {
setInterval(GetMessages(), 1000);
}
}; //<-- Love this semi too!
回答by Arun P Johny
I think you can adapt divide and conquermethodology here. Remove last half of your script and see whether the error is coming if not remove the first portion and see. This is a technique which I follows when I get an issue like this. Once you find the half with the error then subdivide that half further till you pin point the location of the error.
我认为你可以在这里调整分而治之的方法。删除脚本的后半部分,如果不删除第一部分,看看是否会出现错误。当我遇到这样的问题时,这是我遵循的一种技术。一旦你找到有错误的那一半,然后再细分那一半,直到你指出错误的位置。
This will help us to identify the actual point of error.
这将帮助我们确定实际的错误点。
I do not see any problem with this script.
我看不出这个脚本有任何问题。
This may not be the exact solution you want but a way to locate and fix your problem.
这可能不是您想要的确切解决方案,而是一种定位和解决问题的方法。
回答by JayC
Looks like it's being interpreted as being all on one line. See the same results in fiddler2.
看起来它被解释为全部在一行上。在 fiddler2 中看到相同的结果。
回答by ban-geoengineering
This problem could do due to your JS code having comments being minified. If so and you want to keep your comments, then try changing your comments - for example, from this:
这个问题可能是由于您的 JS 代码的注释被缩小了。如果是这样并且您想保留您的评论,请尝试更改您的评论 - 例如,从这里:
// Reset the input box
// Reset the input box
...to...
...到...
/* Reset the input box */
/* Reset the input box */
回答by NVRM
Adding a note: very strangly this error was there very randomly, with everything working fine.
添加一个注释:非常奇怪的是,这个错误是非常随机的,一切正常。
Syntax error missing } after function body | At line 0 of index.html
It appear that I use /**/
and //
with some fancy unicode char in different parts of my scripts for different comments.
这似乎是我使用/**/
,并//
在我的脚本不同意见的不同部分一些花哨的Unicode字符。
This is useful to me, for clarity and for parsing.
为了清晰和解析,这对我很有用。
But if this unicode characterand probably some others are used on a js file in comments before any js execution, the error was spawning randomly.
但是,如果在执行任何 js 之前在注释中的 js 文件中使用了这个unicode 字符和其他一些字符,则错误是随机产生的。
This might be linked to the fact that js files aren't UTF8 before being called and read by the parent page, it is utf8 when DOM ready. Can't tell.
这可能与 js 文件在被父页面调用和读取之前不是 UTF8 的事实有关,当 DOM 准备好时它是 utf8。说不清。
If that can help!
如果那可以帮助!
回答by Michelle Audrey Sanders
"Hm I think I found a clue... I'm using notepad++ and have until recently used my cpanel file manager to upload my files. Everything was fine until I used FireZilla FTP client. I'm assuming the FTP client is changing the format or encoding of my JS and PHP files. – "
“嗯,我想我找到了一个线索......我正在使用记事本++,直到最近才使用我的 cpanel 文件管理器上传我的文件。一切都很好,直到我使用 FireZilla FTP 客户端。我假设 FTP 客户端正在改变我的 JS 和 PHP 文件的格式或编码。 –”
I believe this was your problem (you probably solved it already). I just tried a different FTP client after running into this stupid bug, and it worked flawlessly. I'm assuming the code I used (which was written by a different developer) also is not closing the comments correctly as well.
我相信这是你的问题(你可能已经解决了)。在遇到这个愚蠢的错误后,我只是尝试了一个不同的 FTP 客户端,它运行得很好。我假设我使用的代码(由不同的开发人员编写)也没有正确关闭注释。
回答by Mujahid
Seems there should be added another semi in the following code too
似乎也应该在下面的代码中添加另一个半
// Start checking of messages every second
window.onload = function() {
if(getUrlVars()['to'] != null) {
setInterval(GetMessages(), 1000);
}
}; <---- Semi added
Also here in this code, define the var
top of the function
同样在这段代码中,定义var
函数的顶部
function readCookie(name) {
var i;
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
Hope this will help you
希望能帮到你