Similar to Access VBScript variable within Javascript inside of an HTA I'm trying to pass a variable between two scripts.
When I use the simplified example in the answer on that question, it behaves as described and the variable is available to the Javascript. But when I tried to do the same thing in my code, it does not work.
I think the issue is that the variables I want to give to the Javascript portion of my code are defined inside a sub in the VBScript, and so they are not available globally. I've tried two things - first I tried Dim outside of the sub, this appeared to make the variable available to both scripts, but the values I set inside the VBScript sub did not persist (JavaScript recognizes the variable exists, but says it is 'undefined'). Second, I tried creating a function in the VBScript to explicitly set values to global variables, ran into a bunch of errors, not sure if I was doing something wrong or if this just isn't a viable method.
Below is a stripped down version of my code:
<html>
<head>
<hta:application
APPLICATIONNAME = "Program ALERT"/>
<script type='text/vbscript'>
Sub Window_onload()
Dim cmd, srFSO, srFile, srCount, srMsg
cmd="bcp ""select cast(count(*) as varchar(2)) from database..table where foo = 'bar'"" queryout \\path\file.txt -c -T -S serveraddress"
Set srFSO = CreateObject("Scripting.Filesystemobject")
Set srFile = srFSO.OpenTextFile("\\path\file.txt")
srCount = srFile.Readall
srFile.Close
If srCount = 0 then
Me.SetTimeout "Me.Close()",8000
else
Me.SetTimeout "Me.Close()",0
End If
If srCount = 1 then
srMsg = "There is " + srCount + " open SR."
else
srMsg = "There are " + srCount + " open SRs."
End If
document.getelementbyid("SRs").innerHTML = srMsg
End Sub
Sub ExitProgram
window.close()
End Sub
</script>
<script type="text/javascript">
if (srCount != 0) {
seconds = 8;
}
else {
seconds = 0;
}
function decreaseTime(){
document.getElementById("exitbutton").value="Exit (" + seconds + ")";
seconds--;
if(seconds<0){
document.getElementById("exitbutton").value="Exit (" + seconds + ")";
return true;
}
setTimeout('decreaseTime()',1000);
}
window.onload = function() {
document.getElementById("exitbutton").value="Exit (" + seconds + ")";
setTimeout('decreaseTime()',1000);
}
</script>
</head>
<body scroll="no">
<h1>Title</h1>
<h2><div id="SRs"></div></h2>
<p align="right"><input id=exitbutton type="button" value="Exit" onClick="ExitProgram" class="myButton"></p>
</body>
</html>
Essentially, what is supposed to happen is that a message displays how many records were returned by a SQL query, and then automatically close after 8 seconds. However if the query returned no records then I want it to close immediately.
Before I added the JavaScript portion to draw a countdown on the exit button, it performed just fine. But adding the countdown seems to have forced the application to stay open until the JavaScript countdown ends, even if the SQL query returned no records. So I'm trying to pass the query results to the JavaScript so that it too can modify its countdown starting point to zero when needed.
ifcondition in the Javascript or how can I feed the same data into the Javascript (for the same purpose). The first would be preferable, since the second would basically mean performing double the workload / redundancy on the client side.if..elsein JS is executed at parsing time, at that timesrCounthas no set value (VBS sets the value after window will be loaded), even if it was global in VBS. Secondly,windowhas only oneonloadproperty, only JS handler seems to be executed. Declare the JS load handler as a regular function, and call that from VBS load handler. Then move theif..elseto the ex JS load handler (and makesrCountglobal in VBS ofcourse). Thirdly, never pass a string to JSsetTimeoutas a callback, use a reference instead.