trying submit multiple html forms 1 vbscript function. purpose of function update text file. depending on text area submitted updating should dictate text file function updates. need 1 function handle file updates text areas. here code snippet.
the onload function loads text in text files text areas.
the f1 , f2 functions copy text areas clipboard. simple quick reference clipboard.
you can see how need apply concept every function. several forms submitting single functions (read, write, , copy).
<head> <title>quick comments</title> <hta:application applicationname="quick comments" scroll="yes" > </head> <script language="vbscript"> sub window_onload() set ofso=createobject("scripting.filesystemobject") set ofile=ofso.opentextfile("signature.txt",1) text=ofile.readall document.all.var1.value=text ofile.close set ofile=ofso.opentextfile("initial.txt",1) text=ofile.readall document.all.var2.value=text ofile.close end sub sub fileupdate dim theform set theform = document.field1 set objfiletowrite = createobject("scripting.filesystemobject").opentextfile("signature.txt",2,true) objfiletowrite.writeline(theform.signature.value) objfiletowrite.close set objfiletowrite = nothing end sub sub f1 dim theform set theform = document.field1 set wshshell = createobject("wscript.shell") wshshell.run "cmd.exe /c echo " & theform.signature.value & " | clip", 0, true end sub sub f2 dim theform set theform = document.field2 set wshshell = createobject("wscript.shell") wshshell.run "cmd.exe /c echo " & theform.initial.value & " | clip", 0, true end sub </script> <body> <h1>quick comments</h1><hr> <form id="field1" name="field1"> <textarea name="var1" id='signature' cols=75 rows=8></textarea> <input type="button" name="copy" value="copy" onclick="f1" language="vbscript"> <input type="button" name="update" value="update" onclick="fileupdate" language="vbscript"> </form> <form id="field2" name="field2"> <textarea name="var2" id='initial' cols=75 rows=8></textarea> <input type="button" name="copy" value="copy" onclick="f2" language="vbscript"> <input type="button" name="copy" value="copy" onclick="f1" language="vbscript"> </form> </body>
consider below examples based on code. there separate procs writetextfile
, readtextfile
read-write ops, switched window.clipboarddata.setdata
clipboard control method. suppose text files located in same folder hta, path folder stored in strhtafolder
variable. note <textarea>
tag .name
property used filename update.
first 1 uses window.event.srcelement.parentnode.childnodes(0)
retrieve necessary <textarea>
node current event
object:
<html> <head> <title>quick comments</title> <hta:application id="ohta" applicationname="quick comments" scroll="yes" > </head> <script language="vbscript"> dim strhtafolder sub window_onload() strhtafolder = createobject("scripting.filesystemobject").getparentfoldername(replace(ohta.commandline, """", "")) & "\" document.all.signature.value = readtextfile(strhtafolder & "signature.txt", -2) ' -2 - system default, -1 - unicode, 0 - ascii document.all.initial.value = readtextfile(strhtafolder & "initial.txt", -2) end sub sub fileupdate() dim objform, objtextarea set objform = window.event.srcelement.parentnode set objtextarea = objform.childnodes(0) writetextfile objtextarea.value, strhtafolder & objtextarea.name & ".txt", -2 end sub sub copytext() dim objform, objtextarea set objform = window.event.srcelement.parentnode set objtextarea = objform.childnodes(0) window.clipboarddata.setdata "text", objtextarea.value end sub function readtextfile(strpath, lngformat) ' lngformat -2 - system default, -1 - unicode, 0 - ascii createobject("scripting.filesystemobject").opentextfile(strpath, 1, false, lngformat) readtextfile = "" if not .atendofstream readtextfile = .readall .close end end function sub writetextfile(strcontent, strpath, lngformat) ' lngformat -2 - system default, -1 - unicode, 0 - ascii createobject("scripting.filesystemobject").opentextfile(strpath, 2, true, lngformat) .write (strcontent) .close end end sub </script> <body> <h1>quick comments</h1><hr> <form id="field1" name="field1"> <textarea name="signature" id='signature' cols=75 rows=8></textarea> <input type="button" name="copy" value="copy" onclick="copytext()" language="vbscript"> <input type="button" name="update" value="update" onclick="fileupdate()" language="vbscript"> </form> <form id="field2" name="field2"> <textarea name="initial" id='initial' cols=75 rows=8></textarea> <input type="button" name="copy" value="copy" onclick="copytext()" language="vbscript"> <input type="button" name="update" value="update" onclick="fileupdate()" language="vbscript"> </form> </body> </html>
and second example changed. stores on window load both signature , initial elements array, , later when button pressed passes index of element in array sub when event occurs:
<html> <head> <title>quick comments</title> <hta:application id="ohta" applicationname="quick comments" scroll="yes" > </head> <script language="vbscript"> dim strhtafolder, arritems sub window_onload() arritems = array(document.all.signature, document.all.initial) strhtafolder = createobject("scripting.filesystemobject").getparentfoldername(replace(ohta.commandline, """", "")) & "\" arritems(0).value = readtextfile(strhtafolder & "signature.txt", -2) ' -2 - system default, -1 - unicode, 0 - ascii arritems(1).value = readtextfile(strhtafolder & "initial.txt", -2) end sub sub fileupdate(stritem) writetextfile arritems(stritem).value, strhtafolder & arritems(stritem).name & ".txt", -2 end sub sub copytext(stritem) window.clipboarddata.setdata "text", arritems(stritem).value end sub function readtextfile(strpath, lngformat) ' lngformat -2 - system default, -1 - unicode, 0 - ascii createobject("scripting.filesystemobject").opentextfile(strpath, 1, false, lngformat) readtextfile = "" if not .atendofstream readtextfile = .readall .close end end function sub writetextfile(strcontent, strpath, lngformat) ' lngformat -2 - system default, -1 - unicode, 0 - ascii createobject("scripting.filesystemobject").opentextfile(strpath, 2, true, lngformat) .write (strcontent) .close end end sub </script> <body> <h1>quick comments</h1><hr> <form id="field1" name="field1"> <textarea name="signature" id='signature' cols=75 rows=8></textarea> <input type="button" name="copy" value="copy" onclick="copytext(0)" language="vbscript"> <input type="button" name="update" value="update" onclick="fileupdate(0)" language="vbscript"> </form> <form id="field2" name="field2"> <textarea name="initial" id='initial' cols=75 rows=8></textarea> <input type="button" name="copy" value="copy" onclick="copytext(1)" language="vbscript"> <input type="button" name="update" value="update" onclick="fileupdate(1)" language="vbscript"> </form> </body> </html>
btw, use of <form>
tags isn't necessary, might use e. g. <div>
tags instead.
Comments
Post a Comment