Question

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:

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

When I run it, it gives me this error:

enter image description here

Note that the script runs fine if I save it with ANSI encoding.

Is Windows Script Host engine not compatible with the UTF-8 encoding?

Was it helpful?

Solution

Possible source file encodings for VBScript and JScript are ANSI or UTF-16 (LE). UTF-8 can't be used.

OTHER TIPS

As an alternative, you can save your file as Windows Script File (.wsf). Apparently, the Windows Script Host does accept XML that is encoded in UTF-8. The script from the question would be written in a WSF as:

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

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. 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. But I use this anyway.

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top