简体   繁体   English

通过Windows脚本宿主以UTF-8编码运行JScr​​ipt或VBScript文件的问题

[英]Issues running JScript or VBScript files with UTF-8 encoding thru Windows Script Host

I'm not sure if I'm doing something wrong, but I can't seem to run the following JScript through the Windows Script Host if I save it in a .js file with the UTF-8 encoding: 我不确定我做错了什么,但如果我将它保存在带有UTF-8编码的.js文件中,我似乎无法通过Windows Script Host运行以下JScript:

var name = "Hello world!";
WScript.echo(name);

When I run it, it gives me this error: 当我运行它时,它给了我这个错误:

在此输入图像描述

Note that the script runs fine if I save it with ANSI encoding. 请注意,如果我使用ANSI编码保存脚本,脚本运行正常。

Is Windows Script Host engine not compatible with the UTF-8 encoding? Windows Script Host引擎是否与UTF-8编码不兼容?

As an alternative, you can save your file as Windows Script File ( .wsf ). 或者,您可以将文件另存为Windows脚本文件.wsf )。 Apparently, the Windows Script Host does accept XML that is encoded in UTF-8. 显然,Windows脚本宿主确实接受以UTF-8编码的XML。 The script from the question would be written in a WSF as: 该问题的脚本将在WSF中编写为:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<job>
<script language="JScript"><![CDATA[
    var name = "Hello world!";
    WScript.echo(name);
]]></script>
</job>

Possible source file encodings for VBScript and JScript are ANSI or UTF-16 (LE). VBScript和JScript的可能源文件编码是ANSI或UTF-16(LE)。 UTF-8 can't be used. 不能使用UTF-8。

This works for me: 这对我有用:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<job>
<script language="JScript" src="Your-UTF-8-File.js" />
</job>

First problem is that BOM is necessary in js file. 第一个问题是在js文件中需要BOM。 Second problem is that I have found no way to enable JScript 5.8 mode from WSF, and Chakra does not work from WSF either. 第二个问题是我发现无法从WSF启用JScript 5.8模式,而Chakra也无法使用WSF。 But I use this anyway. 但无论如何我用这个。

UPD. UPD。 I have found a better solution: I have a bootloader which conforms to ugly WScript requirements, and inside of it I am reading files with ADODB.Stream in UTF-8 and eval() them, and this way I can run UTF-8 JavaScript without BOM even on Chakra! 我找到了一个更好的解决方案:我有一个符合丑陋的WScript要求的引导加载程序,在其中我正在使用UTF-8和eval()中的ADODB.Stream读取文件,这样我就可以运行UTF-8 JavaScript即使在Chakra也没有BOM!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM