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

c00*_*0fd 6 windows vbscript wsh utf-8 jscript

我不确定我做错了什么,但是Windows Script Host如果我将它保存在.js带有UTF-8编码的文件中,我似乎无法运行以下JScript :

var name = "Hello world!";
WScript.echo(name);
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它给了我这个错误:

在此输入图像描述

请注意,如果我使用ANSI编码保存脚本,脚本运行正常.

Windows Script Host引擎不兼容的UTF-8编码?

Ekk*_*ner 6

VBScript 和 JScript 可能的源文件编码为 ANSI 或 UTF-16 (LE)。不能使用UTF-8。

  • 有这方面的官方文档吗? (4认同)

Che*_*vel 5

作为替代方案,您可以将文件另存为Windows 脚本文件( .wsf)。显然,Windows 脚本宿主确实接受以 UTF-8 编码的 XML。该问题的脚本将在 WSF 中编写为:

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