【问题标题】:Pass a VBScript variable to Javascript in HTA将 VBScript 变量传递给 HTA 中的 Javascript
【发布时间】:2016-01-18 23:08:40
【问题描述】:

类似于Access VBScript variable within Javascript inside of an HTA 我正在尝试在两个脚本之间传递一个变量。

当我在该问题的答案中使用简化示例时,它的行为与描述的一样,并且该变量可用于 Javascript。但是当我尝试在 my 代码中做同样的事情时,它不起作用。

我认为问题在于我想为代码的 Javascript 部分提供的变量是在 VBScript 中的 sub 内定义的,因此它们在全局范围内不可用。我尝试了两件事 - 首先我在 sub 之外尝试了 Dim,这似乎使变量对两个脚本都可用,但我在 VBScript 子中设置的值没有保留(JavaScript 识别变量存在,但说它是'未定义')。其次,我尝试在 VBScript 中创建一个函数来显式地为全局变量设置值,但遇到了一堆错误,不确定是我做错了什么还是这不是一个可行的方法。

下面是我的代码的精简版:

<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>

本质上,应该发生的是一条消息,显示一条 SQL 查询返回了多少条记录,然后在 8 秒后自动关闭。但是,如果查询没有返回任何记录,那么我希望它立即关闭。

在我添加 JavaScript 部分以在退出按钮上绘制倒计时之前,它执行得很好。但是添加倒计时似乎迫使应用程序保持打开状态,直到 JavaScript 倒计时结束,即使 SQL 查询没有返回任何记录。所以我试图将查询结果传递给 JavaScript,以便它也可以在需要时将其倒计时起点修改为零。

【问题讨论】:

  • 听起来你在这里问了多个问题。 1)为什么变量值不传播? 2)SQL提早结束,为什么页面不关闭?
  • 您提到您尝试将变量放在 Sub 外部的 Dim 中,但在您的代码中,它仍然在 Sub 内部。你能把你的代码贴在 Sub 外面吗?
  • 我想我的问题是 我怎样才能让变量传播,以便我可以在 Javascript 中放置 if 条件 或 如何将相同的数据输入 Javascript(出于相同目的)。第一个更可取,因为第二个基本上意味着在客户端执行双倍的工作负载/冗余。
  • 和其他版本的代码基本一样,只是取Dim这一行,上移3行,成为
  • 一开始,JS中的if..else是在解析的时候执行的,那个时候srCount是没有设置值的(VBS设置了window加载后的值),即使是全局的微博。其次,window 只有一个 onload 属性,似乎只执行了 JS 处理程序。将 JS 负载处理程序声明为常规函数,并从 VBS 负载处理程序调用它。然后将 if..else 移动到前 JS 负载处理程序(并在 VBS 中将 srCount 设为全局)。第三,永远不要将字符串传递给 JS setTimeout 作为回调,而是使用引用。

标签: javascript vbscript hta


【解决方案1】:

使用 Teemu 的建议修改代码:

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2012-01-28
    相关资源
    最近更新 更多