Search code examples
javascriptvbscripthta

How to call a VBScript from javascript


I found a small script on https://www.webdeveloper.com/d/49920-how-to-call-a-javascript-from-vbscript/5 that says you call simply call a VB script function in JavaScript. It also gave this example:

    <HTML>
<Head>
</head>
<Script Language=JavaScript>
function callVB(){

    setTimeout(function(){KeepAwake();}, 5000);

}   
</Script>
<Script Language=VBScript>
    Function KeepAwake()
    set WshShell = CreateObject("WScript.Shell")
            WshShell.SendKeys "{ENTER}"
    End Function
</Script>
<Body>
<input type=button value="Call VB" onclick="callVB()">
</Body>
</HTML>

It works great. After 5 seconds it presses enter. So I put it in my own code. The hta says that the VBS function is undefined despite the VBS code being the same. Error Message

I played around with it for a while but it still wouldn't work and all I could find the internet was the same thing I was doing.

What Am I doing wrong?

Here is my own code

<!DOCTYPE html>

<html>
<head>
<title>The Amazing Nanny Helper</title>
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>
</head>

<p id="retrunValue"></p>

<label for="myfile" id="label" name="label">Select a file:</label>
<input type="file" id="myfile" name="myfile" multiple>
<label for="Start">Start Row:</label>
<input type="number" id="Start" min="0"> 
<button type="button" onclick="do5();">Do 5</button>
<button type="button" onclick="do25();">Do 25</button>
<button type="button" onclick="do100();">Do 100</button>
<button type="button" onclick="do1000();">Do 1000</button>
<button type="button" onclick="do10000();">Do 10000</button>



     <Script Language=JavaScript>
//A bunch of unimportant functions

function do5() {
    FileInput(5);
}
function do25() {
    FileInput(25);
}
function do100() {
    FileInput(100);
}
function do1000() {
    FileInput(1000);
}
function do10000() {
    FileInput(10000);
}
function FileInput(times) {
KeepAwake(); // attempt to call VBS function

// Big loop driven by setTimeouts and functions calling functions that call the calling function
}
//A few more unimportant functions
     </Script>
<Script Language=VBScript>
    Function KeepAwake()
    set WshShell = CreateObject("WScript.Shell")
            WshShell.SendKeys "{ENTER}"
    End Function
</Script>

  </body>


</html>

Solution

  • The problem is with this tag:

    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    

    Here you state that the content should be run with the latest available IE engine. That choice apparently does not support VBScript. You can get support for VBScript with a value like:

    <meta http-equiv="x-ua-compatible" content="ie=ie11" />