Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

javascript - 没有可见的原因导致“意外令牌非法”(No visible cause for “Unexpected token ILLEGAL”)

I'm getting this JavaScript error on my console:

(我在控制台上收到此JavaScript错误:)

Uncaught SyntaxError: Unexpected token ILLEGAL

(Uncaught SyntaxError:意外的令牌非法)

This is my code:

(这是我的代码:)

 var foo = 'bar';? 

It's super simple, as you can see.

(如您所见,它非常简单。)

How could it be causing a syntax error?

(它怎么会引起语法错误?)

  ask by bfavaretto translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error(错误)

When code is parsed by the JavaScript interpreter, it gets broken into pieces called "tokens".

(当JavaScript解释器解析代码时,它会分成称为“令牌”的部分。)

When a token cannot be classified into one of the four basic token types , it gets labelled "ILLEGAL" on most implementations, and this error is thrown.

(当令牌不能分类为四种基本令牌类型之一时 ,在大多数实现中它将被标记为“ ILLEGAL”,并且会引发此错误。)

The same error is raised if, for example, you try to run a js file with a rogue @ character, a misplaced curly brace, bracket, "smart quotes", single quotes not enclosed properly (eg this.run('dev1) ) and so on.

(例如,如果您尝试运行带有流氓@字符,错位的花括号,方括号,“智能引号”,未正确括起单引号的js文件(例如, this.run('dev1) ),则会引发相同的错误等等。)

A lot of different situations can cause this error.

(很多不同的情况都可能导致此错误。)

But if you don't have any obvious syntax error or illegal character, it may be caused by an invisible illegal character.

(但是,如果您没有明显的语法错误或非法字符,则可能是由看不见的非法字符引起的。)

That's what this answer is about.

(这就是答案。)

But I can't see anything illegal!(但是我看不到任何非法的东西!)

There is an invisible character in the code, right after the semicolon.

(分号后的代码中有一个不可见的字符。)

It's the Unicode U+200B Zero-width space character (aka ZWSP , HTML entity ​ ).

(这是Unicode U+200B零宽度空格字符(又名ZWSP ,HTML实体​ )。)

That character is known to cause the Unexpected token ILLEGAL JavaScript syntax error.

(已知该字符会导致Unexpected token ILLEGAL JavaScript语法错误。)

And where did it come from?(它是从哪里来的?)

I can't tell for sure, but my bet is on jsfiddle .

(我不能确定,但??是我的赌注是jsfiddle 。)

If you paste code from there, it's very likely to include one or more U+200B characters.

(如果从那里粘贴代码,则很可能包含一个或多个U+200B字符。)

It seems the tool uses that character to control word-wrapping on long strings.

(看来该工具使用该字符来控制长字符串的自动换行。)

UPDATE 2013-01-07

(更新2013-01-07)

After the latest jsfiddle update , it's now showing the character as a red dot like codepen does.

(在最新的jsfiddle更新之后它现在像codepen一样将字符显示为红点 。)

Apparently , it's also not inserting U+200B characters on its own anymore, so this problem should be less frequent from now on.

(显然 ,它也不再自己插入U+200B字符,因此此问题从现在开始应该不那么频繁了。)

UPDATE 2015-03-17

(更新2015-03-17)

Vagrant appears to sometimes cause this issue as well, due to a bug in VirtualBox .

(由于VirtualBox中的错误, Vagrant有时也会引起此问题。)

The solution, as per this blog post is to set sendfile off;

(根据此博客文章的解决方案是将sendfile off;设置为sendfile off;)

in your nginx config, or EnableSendfile Off if you use Apache.

(在您的nginx配置中,如果您使用的是Apache,请EnableSendfile Off 。)

It's also been reported that code pasted from the Chrome developer tools may include that character, but I was unable to reproduce that with the current version (22.0.1229.79 on OSX).

(据报道 ,从Chrome开发人员工具粘贴的代码可能包含该字符,但我无法在当前版本(OSX上为22.0.1229.79)上重现该字符。)

How can I spot it?(我怎样才能发现它?)

The character is invisible, do how do we know it's there?

(角色是看不见的,我们怎么知道它在那里?)

You can ask your editor to show invisible characters.

(您可以要求编辑器显示不可见的字符。)

Most text editors have this feature.

(大多数文本编辑器都具有此功能。)

Vim, for example, displays them by default, and the ZWSP shows as <u200b> .

(例如,Vim默认显示它们,而ZWSP显示为<u200b> 。)

You can also debug it online: jsbin displays the character as a red dot on its code panes (but seems to remove it after saving and reloading the page).

(您也可以在线调试它: jsbin在其代码窗格上将字符显示为红点(但在保存并重新加载页面后似乎将其删除)。)

CodePen.io also displays it as a dot , and keeps it even after saving.

(CodePen.io还将其显示为点 ,并且即使保存后也将其保留。)

Related problems(相关问题)

That character is not something bad, it can actually be quite useful.

(这个角色还不错,实际上很有用。)

This example on Wikipedia demonstrates how it can be used to control where a long string should be wrapped to the next line.

(Wikipedia上的此示例演示了如何使用它来控制将长字符串包装到下一行的位置。)

However, if you are unaware of the character's presence on your markup, it may become a problem.

(但是,如果您不知道标记中字符的存在,则可能会出现问题。)

If you have it inside of a string (eg, the nodeValue of a DOM element that has no visible content), you might expect such string to be empty, when in fact it's not (even after applying String.trim ).

(如果您将其包含在字符串中(例如,没有可见内容的DOM元素的nodeValue ),则可能期望该字符串为空,而实际上却不是(即使在应用String.trim )。)

ZWSP can also cause extra whitespace to be displayed on an HTML page, for example when it's found between two <div> elements (as seen on this question ).

(ZWSP还会导致多余的空格显示在HTML页面上,例如,当它在两个<div>元素之间找到时(如在此问题上所见)。)

This case is not even reproducible on jsfiddle, since the character is ignored there.

(这种情况在jsfiddle上甚至无法重现,因为在此字符被忽略。)

Another potential problem: if the web page's encoding is not recognized as UTF-8, the character may actually be displayed (as a€? in latin1, for example).

(另一个潜在的问题:如果网页的编码没有被识别为UTF-8,字符可能实际上被显示(如a€?在latin1的,例如)。)

If ZWSP is present on CSS code (inline code, or an external stylesheet), styles can also not be parsed properly, so some styles don't get applied (as seen on this question ).

(如果ZWSP存在于CSS代码(内联代码或外部样式表)上,则样式也无法正确解析,因此某些样式不会得到应用(如本问题所示 )。)

The ECMAScript Specification(ECMAScript规范)

I couldn't find any mention to that specific character on the ECMAScript Specification (versions 3 and 5.1 ).

(在ECMAScript规范(版本35.1 )中,我找不到任何提及该特定字符的内容。)

The current version mentions similar characters ( U+200C and U+200D ) on Section 7.1 , which says they should be treated as IdentifierPart s when "outside of comments, string literals, and regular expression literals".

(当前版本在<a href="https://stackoom.com/link/aHR0cDovL2VjbWEtaW5


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...