Javascript javascript意外标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6439778/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:43:48  来源:igfitidea点击:

javascript unexpected identifier

javascript

提问by Jerodev

I am trying to compress my JavaScript code to get less traffic on my site. It has been working fine, but now I came across an error I can't resolve.

我正在尝试压缩我的 JavaScript 代码以减少我网站上的流量。它一直工作正常,但现在我遇到了一个无法解决的错误。

I turned my ajax function into one line:

我把我的 ajax 函数变成了一行:

function(){if(xmlhttp.readyState==4&&xmlhttp.status==200){document.getElementById("content").innerHTML=xmlhttp.responseText;}}xmlhttp.open("GET","data/"+id+".html",true);xmlhttp.send();}

But the chrome console tells me there is an unexpected identifier on this line. Firefox says there is an semicolon missing on this line.

但是 chrome 控制台告诉我这一行有一个意外的标识符。Firefox 说这一行缺少一个分号。

I have been trying to figure out what is wrong, but I can't find the error, can someone help me with this?

我一直试图找出问题所在,但我找不到错误,有人可以帮我解决这个问题吗?

回答by pimvdb

Yes, you have a }too many. Anyway, compressing yourself tends to result in errors.

是的,你有}太多了。无论如何,压缩自己往往会导致错误。

function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("content").innerHTML = xmlhttp.responseText;
    }
} // <-- end function?
xmlhttp.open("GET", "data/" + id + ".html", true);
xmlhttp.send();
}

Use Closure Compilerinstead.

改用闭包编译器

回答by Daniel Earwicker

I recommend using http://jsbeautifier.org/- if you paste your code snippet into it and press beautify, the error is immediately visible.

我建议使用http://jsbeautifier.org/- 如果您将代码片段粘贴到其中并按下 beautify,错误将立即可见。

回答by pilif

In such cases, you are better off re-adding the whitespace which makes the syntax error immediate apparent:

在这种情况下,最好重新添加空格,这会使语法错误立即显现:

function(){
  if(xmlhttp.readyState==4&&xmlhttp.status==200){
    document.getElementById("content").innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("GET","data/"+id+".html",true);xmlhttp.send();
}

There's a } too many. Also, after the closing } of the function, you should add a ; before the xmlhttp.open()

} 太多了。此外,在函数的关闭 } 之后,您应该添加一个 ; 在 xmlhttp.open() 之前

And finally, I don't see what that anonymous function does up there. It's never executed or referenced. Are you sure you pasted the correct code?

最后,我看不到那个匿名函数在上面做了什么。它从未被执行或引用。你确定你粘贴了正确的代码?

回答by Ankur

Either remove one } from end of responseText;}}or from the end of the line

从行尾responseText;}}或行尾删除一个 }

回答by MUS

It looks like there is an extra curly bracket in the code.

看起来代码中有一个额外的大括号。

function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("content").innerHTML = xmlhttp.responseText;
    }
// extra bracket }
xmlhttp.open("GET", "data/" + id + ".html", true);
xmlhttp.send();
}