3

I'm running javascript in the Windows Script Host, so there is no browser involved.

How do I identify, from within the script, which version of JavaScript its execution environment, WSH, supports? For those who aren't familiar with WSH, it takes as input a "raw" JavaScript script i.e. one with no surrounding <script> tags as HTML-embedded scripts would have. Afaik WSH passes the script straight to its registered javascript engine (JScript.Dll) for execution and no browser is involved at all.

googling only seems to turn up methods which presuppose that the script is executing in a browser, which is not the case for a script executed in WSH.

Cœur
  • 37,241
  • 25
  • 195
  • 267
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • 2
    You're misreading the book: the variables are defined manually earlier in the code. In any case, 4th edition was published 15 years ago, and the code is detecting JS versions that have become long since obsolete. – JJJ Apr 09 '16 at 17:51
  • "defined manually earlier " since you seem to have the book, could you point out where, exactly? Maybe my eyesight is not what it was. Tks ... – MartynA Apr 09 '16 at 17:57
  • I have the e-book so I don't know which code snippet you're talking about exactly, but every example that uses `_version` or `_js12_` starts with something like `` (which won't work on modern environments). – JJJ Apr 09 '16 at 18:00
  • Well, the `_version` is in Example 20-1. But in the WSH, the code doesn't have any surrounding – MartynA Apr 09 '16 at 18:05
  • Mentioning the O'Reilly book was obviously serving as a distraction, so I've edited it out of my q, and replaced it with a bit more explanation of why executing in WSH is different from embedding a script in a web page. – MartynA Apr 09 '16 at 18:29

1 Answers1

2

Take a look at some WSH built-in properties in the following example:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = '';
s = s + 'WScript.Version = ' + WScript.Version + '\n';
s = s + 'WScript.BuildVersion = ' + WScript.BuildVersion + '\n';
s = s + 'ScriptEngineMajorVersion = ' + ScriptEngineMajorVersion() + '\n';
s = s + 'ScriptEngineMinorVersion = ' + ScriptEngineMinorVersion() + '\n';
s = s + 'ScriptEngineBuildVersion = ' + ScriptEngineBuildVersion() + '\n';
s = s + WScript.FullName + ' version = ' + fso.GetFileVersion(WScript.FullName) + '\n';
WScript.Echo(s);

The code gives output for me:

WScript.Version = 5.8
WScript.BuildVersion = 18283
ScriptEngineMajorVersion = 5
ScriptEngineMinorVersion = 8
ScriptEngineBuildVersion = 18231
C:\Windows\System32\WScript.exe version = 5.8.7601.18283

The point is you can retrieve WSH version that ways (though I have no idea about the difference in WScript.BuildVersion and ScriptEngineBuildVersion).

There are WSH versions 1.0, 2.0 (also called 5.1), 5.6, 5.7, 5.8 and 5.812 by the link. Сonsidering JScript version is the same as WSH (except JScript 5.1 is for WSH 2.0, and perhaps - I haven't found any proof - JScript 9.0 is for WSH 5.812, since JScript 9.0 introduced with IE 9 and later WSH 5.812 with Windows 10). You can find JavaScript and JScript versions correspondence by the link 1 and link 2.

Based on that I suppose the following compatibility:

WSH        JavaScript

1.0        1.0
2.0        1.4
5.5-5.8    1.5
5.812      1.8.1 (needs to be checked)
omegastripes
  • 12,351
  • 4
  • 45
  • 96
  • That's great thanks. One minor q while I have your ear: Your script works fine if I run it using WScript.Exe from the command line, but if I run it from inside a WSH window in my (Delphi) app, it complains that `WScript` is undefined. Is there a way of fixing that so that it does run correctly? – MartynA Apr 10 '16 at 07:36
  • @MartynA, WScript object available in WSH script engine only, another engines might utilize it also via emulating or being based on wscript.exe. I suppose your Delphi app has it's own script engine implementation without built-in WScript support, and also having it's JavaScript flavour. Thus it should have another way to retrieve the engine version. If you intend to utilize any WScript methods, you may try to launch temp .js file within separate wscript.exe process, and transfer WScript object to your app from it, but it's a long way, and probably not worth the effort. – omegastripes Apr 10 '16 at 08:50