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.
Revised code using Teemu's suggestions:
<html>
<head>
<hta:application
APPLICATIONNAME = "Program ALERT"/>
<script type='text/vbscript'>
Dim seconds
Sub Window_onload()
Dim cmd, srFSO, srFile, srCount, srMsg, strUser
strUser = CreateObject("WScript.Network").UserName
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 = 1 then
srMsg = "There is " + srCount + " open SR."
else
srMsg = "There are " + srCount + " open SRs."
End If
document.getelementbyid("SRs").innerHTML = srMsg
If srCount = 0 then
Me.SetTimeout "Me.Close()",0
else
seconds = 8
Me.SetTimeout "Me.Close()",8000
Me.setButtonCountdown seconds
End If
End Sub
Sub ExitProgram
window.close()
End Sub
</script>
<script type="text/javascript">
function setButtonCountdown(seconds) {
document.getElementById("exitbutton").value="Exit (" + seconds + ")";
setTimeout('decreaseTime()',1);
}
function decreaseTime(){
document.getElementById("exitbutton").value="Exit (" + seconds + ")";
seconds--;
if(seconds<0){
document.getElementById("exitbutton").value="Exit (" + seconds + ")";
return true;
}
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.