JavaScript调用VBS中的InputBox和MsgBox函数

标签: , , , , ,

严格的是应该是JScript,不过很多不明真相的同学不区分JavaScript和JScript,于是标题就写JavaScript了。JScript和VBScript相互调用搞来搞去都是ScriptControl,一点技术含量也没有。

JavaScript版MsgBox函数,后面两个参数忽略掉,用了那么久VBS我从来没有用过MsgBox最后两个参数。

function MsgBox(prompt, buttons, title)
{
    if(buttons == undefined) buttons = 0;
    if(title == undefined) title = "";

    var sc = new ActiveXObject("MSScriptControl.ScriptControl");
    var code = 'MsgBox("'+ prompt + '","' + buttons + '","' + title + '")';

    sc.Language = "VBScript";
    return sc.Eval(code);
}

JavaScript版InputBox函数,同样去掉了最后两个参数,定义位置的参数也去掉了,如果需要定义位置的话自己加上去好了。

function InputBox(prompt, title, def)
{
    if(title == undefined) title = "";
    if(def == undefined) def = "";
    
    var sc = new ActiveXObject("MSScriptControl.ScriptControl");
    var code = 'Inputbox("'+ prompt + '","' + title + '","' + def + '")';
    
    sc.Language = "VBScript";
    result = sc.Eval(code);
    
    if(result != undefined) return result;
    else return "";
}

简单的测试一下。

var str = InputBox("Enter your name");
if(str != "")
{
    var res = MsgBox("Are you " + str + "?", 4);
    if(res == 6)
        MsgBox("Yes,you are " + str);
    else
        MsgBox("No,you are not " + str);
}

EOF

随机文章:

  1. SDK编程中的窗口居中
  2. Copy Source As Html
  3. 用VBS创建环境变量
  4. jQuery设置ajax请求中的User-Agent
  5. 一个VBS恶作剧程序

3 条评论 发表在“JavaScript调用VBS中的InputBox和MsgBox函数”上

  1. 怒云说道:

    function VBS()
    {
    this.sc=new ActiveXObject(“MSScriptControl.ScriptControl”);
    this.sc.Language = “VBScript”;

    this.Run=function(code)
    {
    return this.sc.Eval(code);
    }

    this.MakeCode=function(str)
    {
    var re=/\”/g;
    str=str.replace(re,'”+Chr(34)+”‘);

    re=/\’/g;
    str=str.replace(re,'”+Chr(39)+”‘);

    re=/\n/g;
    str=str.replace(re,'”+Chr(10)+”‘);

    re=/\r/g;
    str=str.replace(re,'”+Chr(13)+”‘);

    return str;
    }

    this.InputBox=function(prompt, title, def)
    {
    if(title==undefined)title = “”;
    if(def==undefined)def = “”;

    prompt=this.MakeCode(prompt);
    title=this.MakeCode(title);
    def=this.MakeCode(def);

    var code = ‘InputBox(“‘+ prompt + ‘”,”‘ + title + ‘”,”‘ + def + ‘”)’;

    var result = this.sc.Eval(code);

    return result;
    }

    this.MsgBox=function(prompt, buttons, title)
    {
    if(buttons == undefined) buttons = 0;
    if(title == undefined) title = “”;

    prompt=this.MakeCode(prompt);
    title=this.MakeCode(title);

    var code = ‘MsgBox(“‘+ prompt + ‘”,”‘ + buttons + ‘”,”‘ + title + ‘”)’;

    var result=this.sc.Eval(code);
    return result;
    }
    }

  2. jimbowhy说道:

    MSScriptControl.ScriptControl 在我的机子上死也实例化不了:Autumation server can’t create object!

    • 枫谷剑仙说道:

      我今天也遇到这个问题,Notepad++和VbsEdit都可以运行,但系统里直接运行就提示
      microsoft jscript 运行时错误: automation 服务器不能创建对象

      找了半天发现是因为没有64位的ActiveXObject,使用32位才可运行。正确运行方法如:
      C:\WINDOWS\SysWOW64\wscript.exe 登陆.js

留下回复