PHP 的 header 在服务 JavaScript 时使用哪个字符集

Which charset to use with PHP's header when serving JavaScript

通常,我对所有内容都使用 utf-8utf-8 是否可以接受 JavaScript

header('Content-Type: application/javascript; charset=utf-8');

我问的原因是 http://kunststube.net/encoding/ 状态:

What does it mean for a language to support Unicode then? Javascript for example supports Unicode. In fact, any string in Javascript is UTF-16 encoded. In fact, it's the only thing Javascript deals with. You cannot have a string in Javascript that is not UTF-16 encoded. Javascript worships Unicode to the extent that there's no facility to deal with any other encoding in the core language. Since Javascript is most often run in a browser that's not a problem, since the browser can handle the mundane logistics of encoding and decoding input and output.

我应该使用 utf-16 而不是 utf-8 吗?或者这篇文章是关于 JavaScript 如何编码文本的,而不是浏览器如何解码恰好是 JavaScript 的文件文本的?

搜索 Javascript 标准 http://www.ecma-international.org/ecma-262/5.1/

Enable UTF-8 encoding for JavaScript

然而,您看到的是 utf-8 得到了非常广泛的支持,并且尤其适合用于通过网络提供的资源。

是的,你'can'用utf-16、ASCII等编码,但必须被客户端程序支持。

Or does this article pertain to how JavaScript encodes text, not how the browser deencodes the text of a file which happens to be JavaScript?

正确。 JavaScript String 值作为 UTF-16 代码单元序列存储和处理,但这与 JS 源代码的加载方式无关。

UTF-16 是 JS 源代码编码的糟糕选择,因为它不兼容 ASCII。有许多浏览器会破坏 UTF-16 等非 ASCII 字符集,因此通常最好避免将其作为网络上的文件编码。

header('Content-Type: application/javascript; charset=utf-8');

这很好,但请注意,并非所有浏览器都必须遵守此处的 charset;某些(尤其是较旧的)浏览器将使用与链接到脚本的页面上使用的编码相同的编码来解码脚本文件。如果该页面也以 UTF-8 格式提供,没问题。