Access VBScript variable within Javascript inside of an HTA

szdmgdev picture szdmgdev · Dec 23, 2011 · Viewed 9.4k times · Source

I'm trying to access a variable in javascript that i set in vbscript all within one hta file:

<script type="vbscript">
    var globalVariable = test123
</script>

<script type="text/javascript">
    var globalVariable = <%= globalVariable %>;
</script>

This doesn't populate the variable globalVariable with anything. I've also tried

var globalVariable = "<%= globalVariable %>";

This just populates the string within the quotes into the variable globalVariable.

I can't get the value 'test123' set in the VBScript section to populate into a JS variable within the JS section.

Any help would be appreciated.

Answer

Hugh Sweeney picture Hugh Sweeney · Dec 24, 2011

Based on your question, I've written an HTA containing the following code:

<script type="text/vbscript">
dim globalvariable
globalvariable = "test123"
</script>
<script type="text/javascript">
alert(globalvariable);
</script>

and the value "test123" is displayed in the alert box.

Note the following differences with your code:

  • vbs language syntax is different to js syntax;
  • your js code looks like asp code to me, rather than js;
  • I got an error message using '' without 'text/' prefix;