只有IE浏览器才支持这个构造函数,可以用这个来判断,当前是否为IE浏览器
var isIE=!!window.ActiveXObject;
在IE的不同版本下,要创建XHR对象,也需要通过这个构造函数来创建(构造时的参数不一样)。
因为ActiveXObject的使用容易存在安全问题,如IE11中报错
Automation 服务器不能创建对象
解决办法:
Internet选项-安全-自定义安全级别-启用“对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本”
详解
JavaScript 中 ActiveXObject 对象是启用并返回 Automation对象的引用。
newObj = new ActiveXObject( servername.typename[, location])
- servername是必选项。提供该对象的应用程序的名称。
- typename是必选项。要创建的对象的类型或类。
- location是可选项。创建该对象的网络服务器的名称。
附件:https://files.cnblogs.com/files/hellohello/oleview_setup.zip
具体这个new ActiveXObject能创建出来什么对象,去搜索一下“VBS CreateObject”就知道了,后面举出几个例子
取得机器名,登录域及登录用户名
var WshNetwork = new ActiveXObject("WScript.Network"); console.log("Domain = " + WshNetwork.UserDomain); // Domain = YGSOFT console.log("Computer Name = " + WshNetwork.ComputerName); // Computer Name = GRIS-LIWENJUN console.log("User Name = " + WshNetwork.UserName); // User Name = liwenjun
Wscript.shell
获取环境变量
let pnsys = new ActiveXObject("WScript.shell"); let pn = pnsys.Environment("PROCESS"); console.log(pn("WINDIR")); // C:Windows
执行cmd指令(启动了计算器)
var calc=new ActiveXObject("WScript.shell"); calc.Run("calc");
注册表操作(输出400)
var myreadreg = new ActiveXObject("WScript.shell"); try { console.log(myreadreg.RegRead("HKEY_CURRENT_USER\Console\Git Bash\FontWeight")); } catch (e) { console.log("读取的值不存在!"); }
写入注册表
mywritereg.RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\MyTest","c:\mytest.exe");
删除注册表
mydelreg.RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\MyTest");
Scripting.filesystemobject
获取磁盘信息(如C盘)
var fs = new ActiveXObject("scripting.filesystemobject"); d = fs.GetDrive('c'); s = "卷标:" + d.VolumeName; s += "------" + "剩余空间:" + d.FreeSpace / 1024 / 1024 / 1024 + "G"; s += "------" + "磁盘序列号:" + d.serialnumber; console.log(s)
获取文件信息
var myfile = new ActiveXObject("scripting.filesystemobject"); var fi = myfile.GetFile('D:\DemandFlowDeptBO.java'); console.log("文件类型:" + fi.type + "文件大小:" + fi.size / 1024 / 1024 + "M" + "最后一次访问时间:" + fi.DateLastAccessed);
遍历目录
var fso = new ActiveXObject("Scripting.FileSystemObject"); var fldr = fso.GetFolder('D:\vm'); var file = new Enumerator(fldr.files); var folderArr = []; var folder = new Enumerator(fldr.SubFolders); for (; !folder.atEnd(); folder.moveNext()) { var folderStr = String(folder.item()); folderArr.push(String(folder.item()).substring(folderStr.lastIndexOf("\") + 1, folderStr.length)); } console.log(folderArr.join(" "));
遍历文件
var fso = new ActiveXObject("Scripting.FileSystemObject"); var fldr = fso.GetFolder(para); var file = new Enumerator(fldr.files); var fileArr = []; for (; !file.atEnd(); file.moveNext()) { var fileStr = String(file.item()); fileArr.push(String(file.item()).substring(fileStr.lastIndexOf("\") + 1, fileStr.length)); } alert(fileArr.join(" "));
读写文件
function createText1(){ var fso = new ActiveXObject("Scripting.FileSystemObject"); var f1 = fso.createtextfile("c:\myjstest.txt",true); f1.WriteLine("这是内容"); f1.close(); var kk=fso.OpenTextFile("c:\a.txt");//打开文件 while(!kk.atEndOfLine){ document.writeln(kk.readLine());//读取文件,并输出 } } function createText2(){ var fso = new ActiveXObject("Scripting.FileSystemObject"); var f1 = fso.GetFile("c:\myjstest.txt"); }
WbemScripting.SWbemLocator
获取CPU核数
var locator = new ActiveXObject("WbemScripting.SWbemLocator"); function getCpu() { var service = locator.ConnectServer("."); var properties = service.ExecQuery("SELECT * FROM Win32_Processor"); var e = new Enumerator(properties); // 有阻塞 var p = e.item(); return p.LoadPercentage; } let data = getCpu(); console.log(data) // 4