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

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

问题描述



@charsetUTF-8;



我的问题是:



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

如果我发送一个JS文件给我的朋友,我希望他(她)可以从代码本身了解这个JS文件的字符编码,当他(她)开始浏览或编辑这个JS文件。



谢谢!

解决方案

吨。但是,您可以在 脚本标签,将文件导入页面,使用 charset 属性。这必须与您为该文件提供的 Content-Type 中的 charset (如果有)匹配。引用:


charset 属性给出了外部脚本资源的字符编码。如果 src 属性不存在,则不得指定属性。如果属性被设置,其值必须是有效的字符编码名称,必须是的noreferrer> ASCII不区分大小写的匹配该编码的首选MIME名称,并且必须与 charset 参数中给出的编码相匹配外部文件的/TR/html5/fetching-resources.html#content-typerel =noreferrer> Content-Type metadata (如果有的话)。 [IANACHARSET]


重新编辑:


如果我发送一个JS文件给我的朋友,我希望他(她)可以在他(她)开始浏览或编辑这个JS文件时,从代码本身了解这个JS文件的字符编码。


为此,你几乎只需要告诉他/她。如果文件是UTF-8或Windows-1252或ISO 8859-1,不幸的是没有可用的编码文件指示符,所以我将在开头包括一条注释:

  //编码:UTF-8 

但是,如果您使用的是UTF-16或UTF-32,则应该能够让编辑者使用 BOM ,其他编辑者应该看到和理解(如果它们是Unicode感知编辑器)。这通常只适用于您在需要大量多字节字符的文本(语言)中编写注释,如果您对代码的评论比例很高(因为代码是用西方文本写的),尽管当然欢迎使用您喜欢的任何编码。只是说,如果评论与代码的比例很低,那么即使注释在文本中需要大量的四字节字符,因此代码只需要一个字符。 (而在UTF-16中,您的注释中可能有两个字节而不是四个字节的字符,但代码总是需要每个字符两个字节;而在UTF-32中,每个字符为4个字节,所以整体上该文件可能会更大,即使注释占用的空间较小,但在这里,我可能告诉您已经知道的事情远远好于我,如果我正确地猜到您的问题的原因。)


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

@charset "UTF-8";

My question is:

How to declare character encoding in an INDIVIDUAL JS file?

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.

Thank you!

解决方案

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:

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]

Re your edit:

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.

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

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天全站免登陆