TheDude
TheDude

Reputation: 99

hybrid batch/jscript/hta: read 2 inputs in Jscript and pass them to Batch

Good Morning all,

I'am currently writing a small batch/jscript/hta program.

In the following program, I read a text input field, pass the text to the batch and display it there:

<!-- :: Batch section
@echo off
setlocal

for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "result=%%a"
echo End of HTA window, reply: "%result%"
pause
goto :EOF
-->


<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >

<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,400);

function myFunction() {
  var x = document.getElementById("myText").value;  
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  fso.GetStandardStream(1).WriteLine(x);
  window.close();
}

</SCRIPT>
</HEAD>
<BODY>
   <h3>A demonstration of how to access a Text Field</h3>

    <input type="text" id="myText" value="0123">

    <p>Click the "Login" button to get the text in the text field.</p>

    <button onclick="myFunction()">Login</button>

</BODY>
</HTML>

That works fine.

Now my question: How can I read two text input fields and pass them to the batch? I always get errors in the part of Jscript!

Thanks in advance, I hope someone can help me with that.

Upvotes: 2

Views: 324

Answers (1)

MC ND
MC ND

Reputation: 70933

If your need to return more than one value the easiest way is to concatenate them with a delimiter character and properly configure the for /f to split the read line into the separate tokens

<!-- :: Batch section
@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Defined which tokens we need and the delimiter between them
    for /F "tokens=1,2 delims=|" %%a in ('mshta.exe "%~F0"') do (
        set "field1=%%a"
        set "field2=%%b"
    )

    echo End of HTA window, reply: "%field1%" "%field2%"
    pause
    goto :EOF
-->
<HTML><HEAD><HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
    window.resizeTo(374,400);

    function myFunction() {
        new ActiveXObject("Scripting.FileSystemObject")
            .GetStandardStream(1)
            .WriteLine(
                [ // Array of elements to return joined with the delimiter
                      document.getElementById("myText").value
                    , document.getElementById("myText2").value
                ].join('|')
            );
        window.close();
    };
</SCRIPT>
</HEAD>
<BODY>
   <h3>A demonstration of how to access a Text Field</h3>
    <input type="text" id="myText"  value="0123">
    <input type="text" id="myText2" value="4567">
    <p>Click the "Login" button to get the text in the text field.</p>
    <button onclick="myFunction()">Login</button>
</BODY>
</HTML>

Upvotes: 2

Related Questions