User input in JScript

JScript doesn't provides a function like VBScript InputBox function. Also the prompt method known from JavaScript isn't supported in the WSH environment (this method is exposed from Internet Explorer). Thus we need to find another automation object that supports an InputBox method. The main question is: which objects are available in a WSH environment offering an input box function?

Some people propose to use Office programs like Word or Excel and create a VBA sample showing a input box. A WSH script can access a VBA function and retrieve the users input. Unfortunately not all systems have Office 97 already installed. Thus we need an own ActiveX component, that will do the job for us. Within my WSH books project I developed an ActiveX control that provides an WSHInputBox method. The control may be found in the WSHExtend section of my WSH Bazaar. But not all users have already installed this control (because they won't download an 1.4 MB file, or they don't like to use the control for other purposes - installing the control on several hundred workstations can be a pain).

Therefore I tried to find an alternative: Microsoft Internet Explorer. The sample below uses the MS IE 4.0/5.0 to create a InputBox method useable from WSH JScript scripts. Here are the steps how it works:

The WSH listing shown below was written in JScript.

//************************************************
// File:   Input1.js  
// Author: (c) G. Born  
// Source: http://www.borncity.de
// Windows Script Host Sample Script
// This script demonstrates how to implement user input in JScript.
//
// This script requires the Internet Explorer 4.0/5.0!!!
//
// In no way shall the author be liable for any
// losses or damages resulting from the use of this
// program. Use AS-IS at your own risk.
//
// The code is the property of the author. You may
// use the code and modify it, as far as this header
// remains intact. Further updates and other samples
// may be found on my site:
// http://www.borncity.de
//
// The script was developed during my WSH book project 
// "Inside Windows Scripting Host" I wrote for MS Press Germany.
//************************************************
var title = "Born's InputBox for JScript";
var prompt = "Your Name please";
var WSHShell;
var vbOKCancel = 1;       // some helpers
var vbOKOnly = 0
var vbInformation = 64;
var vbCancel = 2;
// our helper function
function makeInputBox ()
 {
// Create our Internet Browser object
 var oIE4 = WScript.CreateObject("InternetExplorer.Application");
 oIE4.left=50;                  // Windows position
 oIE4.top = 100;
 oIE4.height = 300;             // Windows size
 oIE4.width = 600;
 oIE4.menubar = 1;
 oIE4.toolbar = 1;
 oIE4.navigate ("about:blank"); // HTML document with function
 oIE4.visible = 0;              // keep MSIE invisible
 while (oIE4.Busy)   {}         // wait till MSIE is ready
 var doc1 = oIE4.Document;      // get the document object
 doc1.open;                     // open it
                                // write a script
 doc1.writeln ("<HTML><HEAD>");
 doc1.writeln ("<Script LANGUAGE=\"VBScript\"><!--");
 doc1.writeln ("Function InputBox1 (prompt,title, value)");
 doc1.writeln (" InputBox1 = InputBox (prompt, title, value)");
 doc1.writeln ("End Function");
 doc1.writeln ("//-->");
 doc1.writeln ("</Script>");
 doc1.writeln ("</HEAD><BODY></BODY></HTML>");
 doc1.close;                    // close write access
 return oIE4;
 }
function InputBox (obj, prompt, title, x)
 {
 var oIE4doc = obj.Document.Script;
 var result = oIE4doc.InputBox1(prompt,title,x);
 return result;
 }
{  
// Create our shell object - needed to show messages
WSHShell = WScript.CreateObject("WScript.Shell");
var mobj = makeInputBox ();
var result = InputBox (mobj, prompt,title,"Born");
WSHShell.Popup("Your input was: " + result,
                0,
                "Result",
                vbOKOnly + vbInformation );
                // 2nd try
result = InputBox (mobj, prompt,title,"Bach");
WSHShell.Popup("Your input was: " + result,
                0,
                "Result",
                vbOKOnly + vbInformation );
mobj.Quit();         // close Explorer object
}
//*** End

The script uses a function makeInputBox containing all the nessessary statements to invoke the MS IE 4.0/5.0 and "load" the InputBox1 function (which is a new method exposed from the browser) into the Browser. Calling this function from a WSH script creates an (invisible copy of) the MSIE 4 that provides an external function InputBox1. The makeInputBox used in the JScript script returns the object instance that may be used to reference the InputBox method. Using the new method is simple:

var mobj = makeInputBox ();
var result = InputBox (mobj, prompt,title,"Born");

The makeInputBox method returns an object variable pointing to the Internet Explorer Script object. Then you may call the InputBox method provided from this object.

You can make several calls to the new object. Before you close your script, you should quit also the MS IE 4.0/5.0 using the command:

mobj.Quit();          // close Explorer object 

That's all (and without self written ActiveX controls). We can use this script to query an user input either from JScript or from VBScript. Additional information about this sample and the way how to create a GUI for WSH scripts (including real forms - yes, it is possible) may be found in my German WSH book Inside Windows Scripting Host, published by Microsoft Press. The WSHExtend ActiveX control with a WSHInput method is also available from my WSH Bazaar.

Back

(c) Günter Born