如何在 INDIVIDUAL JS 文件中声明字符编码? [英] How to declare character encoding in an INDIVIDUAL JS file?

查看:40
本文介绍了如何在 INDIVIDUAL JS 文件中声明字符编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以通过以下代码在 INDIVIDUAL CSS 文件中声明字符编码:

We can declare the character encoding in an INDIVIDUAL CSS file by codes below:

@charset "UTF-8";

我的问题是:

如何在 INDIVIDUAL JS 文件中声明字符编码?

How to declare character encoding in an INDIVIDUAL JS file?

如果我给朋友发一个JS文件,希望他(她)在浏览或编辑这个JS文件的时候,能够自己从代码中理解这个JS文件的字符编码.

If I send a JS file to my friend, I hope he (she) can understand this JS file's character encoding from codes themselves when he (she) starts to browse or edit this JS file.

谢谢!

推荐答案

你不能.但是,您可以在 脚本中定义它 标记 使用 charset 属性.这必须与您提供文件的 Content-Type 中的 charset 匹配(如果有).引用:

You can't. You can, however, define it in the script tag that brings the file into the page, using the charset attribute. This must match the charset, if any, in the Content-Type that you serve the file with. Quoting:

charset 属性给出了外部脚本资源的字符编码.如果 src 属性不存在,则不得指定该属性.如果设置了属性,其值必须是有效的字符编码名称,必须是 的noreferrer">ASCII 不区分大小写匹配该编码的首选 MIME 名称,并且必须与 外部文件的内容类型元数据,如果有的话.[IANACHARSET]

The charset attribute gives the character encoding of the external script resource. The attribute must not be specified if the src attribute is not present. If the attribute is set, its value must be a valid character encoding name, must be an ASCII case-insensitive match for the preferred MIME name for that encoding, and must match the encoding given in the charset parameter of the Content-Type metadata of the external file, if any. [IANACHARSET]

重新

如果我给我的朋友发送一个JS文件,我希望他(她)在他(她)开始浏览或编辑这个JS文件时,能够自己从代码中理解这个JS文件的字符编码.

If I send a JS file to my friend, I hope he (she) can understand this JS file's character encoding from codes themselves when he (she) starts to browser or edit this JS file.

为此,您几乎只需告诉他/她即可.如果文件是 UTF-8 或 Windows-1252 或 ISO 8859-1,不幸的是没有可用编码的文件内指示符,所以我会在开头加上注释:

For that, you'll pretty much just have to tell him/her. If the file is in UTF-8 or Windows-1252 or ISO 8859-1, unfortunately there's no in-file indicator of the encoding available, so I'd include a comment at the beginning along the lines of:

// Encoding: UTF-8

如果您使用的是 UTF-16 或 UTF-32,您应该能够告诉您的编辑器使用 BOM,其他编辑器应该看到和理解(如果他们是 Unicode 感知编辑器).这通常仅适用于您使用需要大量多字节字符的文本(语言)编写评论,并且如果您的评论与代码的比率很高(因为代码是用西方文本编写的),尽管当然欢迎您使用任何您喜欢的编码.只是如果注释与代码的比例很低,即使注释位于需要大量四字节字符的文本中,您也可能最好坚持使用 UTF-8,因为代码每个字符只需要一个字节.(而在 UTF-16 中,您的注释中可能会有更多的两字节而不是四字节字符,但代码总是需要每个字符两个字节;而在 UTF-32 中,每个字符需要四个字节.所以总的来说即使评论占用的空间更少,文件也可能更大.但在这里,我可能会告诉你一些你已经知道的比我更了解的事情,如果我正确地猜测了你提出这个问题的原因.)

If you're using UTF-16 or UTF-32, though, you should be able to tell your editor to use a BOM, which other editors should see and understand (if they're Unicode-aware editors). This would typically only apply if you were writing your comments in a text (language) requiring lots of multi-byte characters, and if you have a high ratio of comments to code (since the code is written with western text), although of course you're welcome to use any encoding you like. It's just that if the ratio of comments to code is low, you're probably better off sticking with UTF-8 even if the comments are in a text requiring lots of four-byte characters, because the code will only require one byte per character. (Whereas in UTF-16, you might have more two-byte instead of four-byte characters in your comments, but the code would always require two bytes per character; and in UTF-32, four bytes per character. So on the whole the file may well be larger even though the comments take less space. But here I'm probably telling you things you already know far better than I, if I'm guessing correctly about your reasons for the question.)

这篇关于如何在 INDIVIDUAL JS 文件中声明字符编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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