“\ u2028”unicode字符上的Javascript解析错误 [英] Javascript parse error on '\u2028' unicode character

查看:133
本文介绍了“\ u2028”unicode字符上的Javascript解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在我的javascript源代码中使用\ u2028字符文字,内容类型设置为text / html; charset = utf-8时,我会收到javascript解析错误。

Whenever I use the \u2028 character literal in my javascript source with the content type set to "text/html; charset=utf-8" I get a javascript parse errors.

示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>json</title>

    <script type="text/javascript" charset="utf-8">
    var string = '
    ';
    </script>
</head>
<body>

</body>
</html>

如果< meta http-equiv> 遗漏一切按预期工作。我在Safari和Firefox上测试了这个,两者都表现出同样的问题。

If the <meta http-equiv> is left out everything works as expected. I've tested this on Safari and Firefox, both exhibit the same problem.

关于为什么会发生这种情况的任何想法以及如何正确解决这个问题(不删除编码) ?

Any ideas on why this is happening and how to properly fix this (without removing the encoding)?

编辑:
经过一些研究后,具体问题是使用JSONP返回问题字符。然后由浏览器解释,它将u2028作为换行符读取并抛出有关字符串中无效换行符的错误。

After some more research, the specific problem was that the problem character was returned using JSONP. This was then interpreted by the browser, which reads u2028 as a newline and throws an error about an invalid newline in a string.

推荐答案

是的,它是ECMAScript标准(第3版第7.3节)中记录的JavaScript语言的一个特性,即U + 2028和U + 2029字符计为行结尾。因此,JavaScript解析器将以与换行符相同的方式处理任何未编码的U + 2028/9字符。由于你不能在字符串文字中添加换行符,因此会出现语法错误。

Yes, it's a feature of the JavaScript language, documented in the ECMAScript standard (3rd edition section 7.3), that the U+2028 and U+2029 characters count as line endings. Consequently a JavaScript parser will treat any unencoded U+2028/9 character in the same way as a newline. Since you can't put a newline inside a string literal, you get a syntax error.

这是JSON设计中的一个不幸的疏忽:它实际上不是适当的JavaScript子集。原始U + 2028/9字符在JSON中的字符串文字中有效,并且将被 JSON.parse 接受,但在JavaScript本身中则不然。

This is an unfortunate oversight in the design of JSON: it is not actually a proper subset of JavaScript. Raw U+2028/9 characters are valid in string literals in JSON, and will be accepted by JSON.parse, but not so in JavaScript itself.

因此,如果您确定明确地 \ u -escape这些字符,那么使用JSON解析器生成JavaScript代码是安全的。有些人有,有些则没有;许多 \u -escape所有非ASCII字符,这可以避免这个问题。

Hence it is only safe to generate JavaScript code using a JSON parser if you're sure it explicitly \u-escapes those characters. Some do, some don't; many \u-escape all non-ASCII characters, which avoids the problem.

这篇关于“\ u2028”unicode字符上的Javascript解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆