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

查看:31
本文介绍了'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.

推荐答案

是的,这是 JavaScript 语言的一个特性,记录在 ECMAScript 标准(第 3 版第 7.3 节)中,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.

因此,如果您确定使用 JSON 解析器明确地 u 转义这些字符,那么使用 JSON 解析器生成 JavaScript 代码才是安全的.有些会,有些不会;许多 u - 转义所有非 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天全站免登陆