Is it possible to send HTAREPLY with javascript without window.close()

折月煮酒 提交于 2020-01-02 12:26:05

问题


I am building an HTA/Batch script that includes an interface for a user to send an htareply to the batch script that will send a link to a server to execute a build script on Jenkins. Everything works great but I was wondering if I can leave the hta window open so the user can select another option if required. Any ideas?

I attempted to remove the window.close() from the javascript but then it does not send the htareply to the batch script.

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

echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
PAUSE
goto :EOF

-->


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

<TITLE>HTA Radio Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(440,170);

var reply = "No button selected";
function sendreply(){
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  fso.GetStandardStream(1).WriteLine(reply);
  //window.close();
}

</SCRIPT>
</HEAD>
<BODY>
<p>Select an option.</p>
<label><input type="radio" name="option1" onclick="reply=this.value" 
value="option1">Option1</label>
<label><input type="radio" name="option2" onclick="reply=this.value" 
value="option2">Option2</label>
<label><input type="radio" name="option3" onclick="reply=this.value" 
value="option3">Option3</label>
<br><br>
<button onclick="sendreply()">Submit</button>
<button onclick="window.close()">Close</button>
</BODY>
</HTML>

What I would like to happen is if the user selects and option and submits, the hta window remains open until they select "close".


回答1:


The for /F command works this way: first execute the nested command, collect all its output and wait for it to terminate; then start to execute the commands in the for body with the collected lines. For this reason, you can not use for /F if you want simultaneous (parallel) execution...

You may use a pipe with the HTA part in the left side and Batch part in the right side. However, when set /P command read input from a pipe, it presents a strange behavior...

The solution is to use an auxiliary file, so HTA part write to it and Batch part read from it:

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

rem Initialize Batch side interface the first time
if "%~1" equ "interface" goto :interface

rem Empty pipe file
cd . > pipeFile.txt

echo Select an option:

rem Start HTA-Batch interface
mshta.exe "%~F0" >> pipeFile.txt  |  "%~F0" interface < pipeFile.txt
PAUSE
del pipeFile.txt
goto :EOF


:interface

set "HTAreply="
set /P "HTAreply="
if "%HTAreply%" equ "" goto interface

echo Reply: "%HTAreply%"
if "%HTAreply%" neq "EOF" goto interface
echo End of HTA window

goto :EOF

-->


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

<TITLE>HTA Radio Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(440,170);

var reply = "No button selected";
var fso = new ActiveXObject("Scripting.FileSystemObject");
function sendreply(){
  fso.GetStandardStream(1).WriteLine(reply);
  //window.close();
}

</SCRIPT>
</HEAD>
<BODY>
<p>Select an option.</p>
<label><input type="radio" name="option1" onclick="reply=this.value" 
value="option1">Option1</label>
<label><input type="radio" name="option2" onclick="reply=this.value" 
value="option2">Option2</label>
<label><input type="radio" name="option3" onclick="reply=this.value" 
value="option3">Option3</label>
<br><br>
<button onclick="sendreply()">Submit</button>
<button onclick="fso.GetStandardStream(1).WriteLine('EOF'); window.close()">Close</button>
</BODY>
</HTML>

In this solution HTA and Batch parts are started via a pipeline | just to run both in parallel; remember that the communication between the two parts is performed via the pipeFile...



来源:https://stackoverflow.com/questions/56650624/is-it-possible-to-send-htareply-with-javascript-without-window-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!