gpt4 book ai didi

javascript - 升级 IIS/Classic ASP Javascript/JScript 脚本引擎(到 Chakra?)

转载 作者:行者123 更新时间:2023-12-05 01:46:38 25 4
gpt4 key购买 nike

引用微软的话说,javascript 现在是 Visual Studio 和“通用 Windows 平台”中的一等公民,但我还没有找到一种方法来升级 IIS/Classic ASP 脚本中使用的十年以上的旧 JScript 引擎。所以,我的问题是,有谁知道是否有办法做到这一点?

为什么?

例如,我想在经典的 ASP 页面(使用 javascript 而不是 VBScript)中使用 JSON.parse。目前,我包括了一份 Crockford 的旧 json 脚本,这没问题,但现在应该没有必要了。

最佳答案

为什么?好吧,正如您可能知道的那样,默认情况下没有启用 Chakra 的主机。根据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.

据我所知,这意味着您必须编写自己的自定义脚本执行主机,才能使用 Chakra 评估现有代码。 -_-

虽然这种混搭听起来非常吸引人,但从其他地方克隆您需要的任何对象和方法要容易得多。 htmlfile COM 对象可以公开当前脚本宿主不可用的对象和方法,只需强制其进入兼容模式即可。

// classic WSH JScript version
var htmlfile = new ActiveXObject('htmlfile'), JSON;
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
htmlfile.close(JSON = htmlfile.parentWindow.JSON);

瞧!现在你可以 JSON.parse()JSON.stringify()直到您心满意足为止,无需包含 json2.js,也无需费力调用 IActiveScript::SetProperty .

关于上面的代码片段的快速说明:htmlfile.write('<meta... etc />')在 Classic JScript 中工作,但 .NET 主机与 write() 斗争和 writeln()出于某种原因的方法。 IHTMLDocument2_write()IHTMLDocument2_writeln()如果您切换到 .aspx 和 JScript.NET,应该改用它。

// JScript.NET version
var htmlfile:Object = new ActiveXObject('htmlfile'), JSON:Object = {};
htmlfile.IHTMLDocument2_write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
htmlfile.close(JSON = htmlfile.parentWindow.JSON);

我还想指出,其他更现代的 ECMAscript 方法也可以用类似的方式导入。这里有一些其他方法的演示 aren't natively available in JScript 5.7但可以从 htmlfile 克隆在 IE9 标准模式下。用 .asp 扩展名保存它,在您的网络浏览器中访问它:

<%@ Language="JScript" %>
<h3>Output:</h3>
<textarea style="width: 100%; height: 5em"><%
var htmlfile = Server.CreateObject('htmlfile');
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');

// expose more modern methods from htmlfile
var JSON = htmlfile.parentWindow.JSON;
String.prototype.trim = htmlfile.parentWindow.String.prototype.trim;
Array.prototype.indexOf = htmlfile.parentWindow.Array.prototype.indexOf;
Array.prototype.forEach = htmlfile.parentWindow.Array.prototype.forEach;
Object.keys = htmlfile.parentWindow.Object.keys;

htmlfile.close(); // no longer needed

// demonstrate JSON.parse() and String.trim()
var strJSON = '{ "item1": " val1 needs trimmed. " }';
var objFromJSON = JSON.parse(strJSON);
Response.Write('JSON and String.trim() demo result: ' + objFromJSON.item1.trim() + '\n');

// demonstrate Array.indexOf()
var arr = [2, 4, 6, 8, 10];
Response.Write('Array.indexOf(val) demo result: ' + arr.indexOf(4) + '\n');

// demonstrate Object.keys() and Array.forEach()
var demo = { "foo": "bar", "baz ": "qux" };
demo.getKey = function(val) {
var obj = this, result;
Object.keys(obj).forEach(function(i) {
if (obj[i] === val) result = i;
});
return result;
}
Response.Write('Object.keys(obj).forEach(fn) demo result: ' + demo.getKey('qux'));
%></textarea>

输出:

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

关于javascript - 升级 IIS/Classic ASP Javascript/JScript 脚本引擎(到 Chakra?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34882591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com