Upgrading IIS/Classic ASP Javascript/JScript Scripting Engines (to Chakra?)

后端 未结 2 881
轻奢々
轻奢々 2020-12-10 09:36

Microsoft are quoted as saying that javascript is now a first class citizen in Visual Studio and the \"Universal Windows platform\" but I have yet to find a way of upgrading

2条回答
  •  我在风中等你
    2020-12-10 10:24

    Why? Well, as you probably know, hosts with Chakra available don't have it enabled by default. According to MSDN documentation:

    Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty.

    From what I've been able to understand, this means you'd have to code your own custom script execution host to evaluate your existing code with Chakra. -_-

    As thoroughly enthralling as such a kludge sounds, it's much easier to clone whatever object and methods you need from elsewhere. The htmlfile COM object can expose objects and methods not available to the current script host, simply by forcing it into a compatibility mode.

    // classic WSH JScript version
    var htmlfile = new ActiveXObject('htmlfile'), JSON;
    htmlfile.write('');
    htmlfile.close(JSON = htmlfile.parentWindow.JSON);
    

    And voila! Now you can JSON.parse() or JSON.stringify() till your heart's content, without having to include json2.js, and without having to go through the enormous effort of invoking IActiveScript::SetProperty.

    A quick note about that code snippet above: htmlfile.write('') works in Classic JScript, but .NET hosts struggle with the write() and writeln() methods for some reason. IHTMLDocument2_write() and IHTMLDocument2_writeln() should be used instead if you ever switch to .aspx and JScript.NET.

    // JScript.NET version
    var htmlfile:Object = new ActiveXObject('htmlfile'), JSON:Object = {};
    htmlfile.IHTMLDocument2_write('');
    htmlfile.close(JSON = htmlfile.parentWindow.JSON);
    

    I'd also like to point out that other more modern ECMAscript methods can be imported in a similar way. Here's a demo of a few other methods that aren't natively available in JScript 5.7 but can be cloned from htmlfile in IE9 standards mode. Save this with an .asp extension visit it in your web browser:

    <%@ Language="JScript" %>
    

    Output:

    Output:

    JSON and String.trim() demo result: val1 needs trimmed.
    Array.indexOf(val) demo result: 1
    Object.keys(obj).forEach(fn) demo result: baz
    

提交回复
热议问题