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

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

问题描述

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



@charsetUTF-8;



我的问题是:



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

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



谢谢!

解决方案

t。不过,您可以在 脚本中定义代码,将文件导入网页中,使用 charset 属性。这必须与您在文件中提供的 Content-Type 中的 charset 引用:


charset 属性提供外部脚本资源的字符编码。如果 src 属性不存在,则不能指定属性。如果设置了属性,则其值必须是有效的字符编码名称,必须是 ASCII该编码的首选MIME名称的不区分大小写匹配 ,且必须与 charset 参数中给出的编码相匹配#content-type>内容类型元数据,如果有的话。 [IANACHARSET]


重新编辑:


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


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

  //编码:UTF-8 

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


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