14

How to open a prompt dialog box in WSH usig JScript??

The only pop-up dialog I've found in the doc is the WshShell.Popup() method. But I need a way to request the user to enter a string, like the window.prompt() method in DOM.

Thanks.

GetFree
  • 40,278
  • 18
  • 77
  • 104
  • 2
    Hi welcome to stackoverflow! It's not necessary to put [WSH][JScript] in your question title. – Albert Feb 10 '09 at 15:51

2 Answers2

18

I think the WScript object does not provide such a method however you can show an input box from vbscript running on WSH. So here is one possible solution which lets you call that VB function from within JS! Please note the file extension for the following code fragment ".wsf".

<!-- Test.wsf -->
<job id="InputBoxInJS">
   <script language="VBScript">
      Function VBInputBox(promptText)
        VBInputBox = InputBox(promptText)
      End Function
   </script>

   <script language="JScript">
      WScript.Echo("Hello from JScript")
      var x = VBInputBox("Enter text")
      WScript.Echo(x)
   </script>
</job>
Sandeep Datta
  • 28,607
  • 15
  • 70
  • 90
  • Thanks. This should not depend on the language you use though, but on the environment where you are using the language. This looks like a bad design issue. Just a thought. – GetFree Feb 11 '09 at 03:54
12

I know this question has been answered, but I wouldn't want to use the .wsf stuff and I also wouldn't want the overhead of loading internet explorer (as I've seen other solutions do). I found this solution using Google that I think is the most elegant:

http://with-love-from-siberia.blogspot.com/2009/12/msgbox-inputbox-in-jscript.html

The key is using the ActiveXObject "ScriptControl", setting the language to VBScript and then using the ScriptObject.eval() function. The example on the site stands on its own.

EDIT: For those encountering an error with 64 bit or line feed, etc., there's this improved version with instructions on how to run it (on systems like Win7 x64) here.

Raymond Tau
  • 3,429
  • 26
  • 28
aikeru
  • 3,773
  • 3
  • 33
  • 48
  • The question has nothing to do with browsers. It's about Windows scripting under the Windows Script Host. – GetFree Jan 29 '12 at 22:43
  • 3
    @GetFree I'm pretty sure aikeru knows that - a browser can be invoked from WSH as a horrible workaround for some stuff is all he is saying. –  Feb 15 '12 at 15:04
  • Yes, Jack Douglas is correct. My point is that while it is possible to use ActiveX and Internet Explorer to create a javascript popup and I've seen people do this -- but I think it's awful, lots of overhead, lots of potential for unforeseen issues. I wanted to present an alternative to both that and WSF (which is the selected answer). If my answer is unclear, I welcome suggestions or perhaps even an 'edit'. :) – aikeru Feb 15 '12 at 16:10
  • 1
    +1 Wow! This solution allows PURE JScript code to call VBScript functions! This point is much more important than the answer to this topic about dialog boxes... – Aacini Jun 18 '13 at 22:33