JScript – Using InputBox & MsgBox


Over the years, I have seen many different techniques for emulating the InputBox and MsgBox functions in JScript, but none of them were quite as elegant as the one I saw here. Once I dug into the code and realized that ScriptControl was making it so that JScript could run VBScript commands, I felt a whole new world of possibilities had been found. Therefore, my first test was to make a better functioning InputBox and MsgBox function available to JScript by using the following code:

(function(vbe) {
  vbe.Language = "VBScript";
  vbe.AllowUI = true;

  var constants = "OK,Cancel,Abort,Retry,Ignore,Yes,No,OKOnly,OKCancel,AbortRetryIgnore,YesNoCancel,YesNo,RetryCancel,Critical,Question,Exclamation,Information,DefaultButton1,DefaultButton2,DefaultButton3".split(",");
  for(var i = 0; constants[i]; i++) {
    this["vb" + constants[i]] = vbe.eval("vb" + constants[i]);

  InputBox = function(prompt, title, msg, xpos, ypos) {
    return vbe.eval('InputBox(' + [
        xpos != null ? xpos : "Empty",
        ypos != null ? ypos : "Empty"
      ].join(",") + ')');

  MsgBox = function(prompt, buttons, title) {
    return vbe.eval('MsgBox(' + [
        buttons != null ? buttons : "Empty",
      ].join(",") + ')');

  function toVBStringParam(str) {
    return str != null ? 'Unescape("' + escape(str + "") + '")' : "Empty";
})(new ActiveXObject("ScriptControl"));

What makes this code better is the fact that it will accept any string (even those with special characters). It is also better because it allows for null parameters to be passed to the VBScript functions. Finally, this script makes all of the MsgBox constants such as vbRetryCancel and vbInformation available. The following are example calls made to the defined functions:

var name = InputBox('I am "Script-101".\nWhat is your name?', "Name");
var greetings = name
  ? 'Nice to meet you "' + name + '".'
  : "That's fine, you don't have to tell me who you are.";
MsgBox(greetings, name ? vbInformation : vbCritical, "Greetings");

You can download the JScript file that contains these two code blocks and then try to run it on your Windows PC.  If you are running a 32bit system, this script will work as expected. On the other hand, if you are running a 64bit version of Windows, this script will error out with the message “Automation server can’t create object”.  The reason for this is that “ScriptControl”, which is being used to allow JScript to call VBScript functions, isn’t available in the 64bit version of cscript or WScript.  Therefore, to run this script, you will have to do so using the 32bit version.  You can do this by doing the following:

  1. Go to the start menu
  2. If you are not using Windows Seven, click “Run”
  3. type %windir%\SysWoW64\cmd.exe
  4. Click OK
  5. Now type cscript /path/to/the/script.vbs (change the path to whatever the path to the script is)


2 thoughts on “JScript – Using InputBox & MsgBox”

  1. Hi Chris,

    This is good continuation of the investigation started in my article. Your report on issues with special characters in arguments are very appreciable. I will fix them shortly.

    And here are my five cents to your contribution. These lines from 6 through 8 would be considered better if they would be as follows:

      for(var i = 0; constants[i]; i++) {
        var c = 'vb' + constants[i];
        this[c] = vbe.eval(c);

Leave a Reply

Your email address will not be published. Required fields are marked *

three × = 24

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">