質問

This is more of an informational post, rather than a question. But I'll leave an open ended question; if you can improve on the idea please share. ~EDIT

Is there a better way to store a variable in an HTA? I have come up with this method and it works, but I'm not sure if it's the best way to do this;

Preface: I'm new to coding.

I have been working on a GUI (wrapped in HTA) that is underpinned by VBScript. I was looking for a way to dynamically store variables that I could easily call in other subs/functions independently. This is what I came up with;

psuedo code:

Function StoreVar()
    If somecondition = "someexpectation" Then
      source.InnerHTML = "<a name='myVar' value='var1'></a>"
    Else 
      source.InnerHTML = "<a name='myVar' value='var2'></a>"
    End If
End Function

The markup;

<div id="source"></div>
<input type="button" name="Show Variable" onclick="CallVar()" />

Calling the stored variable;

Function CallVar()
   MsgBox myVar.value
End Function

Fairly straight forward, but uses a little trickery since you never see the actual variable being stored since the div that it is going into does not appear in the GUI. The only affect that it may have on the GUI is the actual placement of the div in the html. I usually put these div's at the end of my html body.

Anyways, I just officially joined. I've been using this site since I started coding (not too long), but I figured that I would start contributing.

役に立ちましたか?

解決

With VB Script you can just define a global variable then you can call it and change it from anywhere in the code, even in Javascript if you added that to the hta.

<script language="VBScript" type="text/vbscript">
Dim MyValue

Sub storeValue(theValue)
    If theCondition Then
        MyValue = theValue
    Else
        MyValue = "Something Else"
    End If
End Sub

Sub retrieveValue
    MsgBox MyValue
End Sub 
</script>

<script language="javascript" type="text/javascript">
    function clearValue() {
        MyValue = ""
    }
</script>

Edit: I wanted to edit to say, I'm actually doing what you mentioned for some of my variables in an HTA using, except I'm storing the value of InnerHTML as the "value" and my div is set to "display:none."

<div id=theDiv style="display:none"></div>
Then
theDiv.InnerHTML = MyValue or MyValue = theDiv.InnerHTML etc

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top