1

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.

7
  • It sounds like you are asking multiple questions here. 1) Why isn't the variable value propagating? 2) Why isn't the page closing if the SQL finishes early? Commented Jan 18, 2016 at 23:15
  • 1
    You mention that you tried putting the variable in a Dim outside the Sub, but in your code here, it is still inside the Sub. Could you post you code with it outside the Sub? Commented Jan 18, 2016 at 23:16
  • I guess my question is how can I get the variable to propagate so I can put an if condition 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. Commented Jan 19, 2016 at 1:02
  • And the other version of the code was basically the same, just take the Dim line and move it up 3 lines to be the first thing after the <script> tag is opened. Commented Jan 19, 2016 at 1:06
  • At first, if..else in JS is executed at parsing time, at that time srCount has no set value (VBS sets the value after window will be loaded), even if it was global in VBS. Secondly, window has only one onload property, 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 the if..else to the ex JS load handler (and make srCount global in VBS ofcourse). Thirdly, never pass a string to JS setTimeout as a callback, use a reference instead. Commented Jan 19, 2016 at 8:36

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.