Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling *.vbs file from html onClick event

Status
Not open for further replies.

lauraSatellite

Technical User
Jul 22, 2004
35
IE
Hi, i have been trying to work out how to call a .vbs file from an onClick event on a html page.

I have come accross many sites that show you how to write a html page which includes a vbscript, but none that shows you how to refer to a vbscript located outside of that html file.

Say I had a file called test.vbs and it was located in the same folder as index.html. Say I wanted test.vbs to execute when a button in index.html is clicked. Can this be done?
 
you might be forced to use the WshShell.Run method

WshShell.Run "c:\test.vbs", 1, True

however there might be a smarter way of doing in html???
im not that familar with all things web, sorry
 
thanks, i will try that and see how it goes.

yeah there is definately a smarter way to go about what i am trying to do, but unfortunately im not that read-up or practiced on vbscript, so something that works will do me just fine for now :)
 
hey, i tried what you suggested:

<HTML>
<HEAD>
<SCRIPT LANGUAGE="vbscript">
<!--
Sub cmdButton1_OnClick
WshShell.Run "C:\test.vbs", 1, True
End Sub

-->
</SCRIPT>
</HEAD>


<BODY BGCOLOR="white">
<INPUT TYPE="button" NAME="cmdButton1" VALUE="Run Script">
</BODY>
</HTML>

but it comes up with an error on the page and doesnt run the script...any ideas?
 
you will need to create an instance of the WshShell object as well so, try
<!--
Sub cmdButton1_OnClick
Set WshShell = WScript.CreateObject( "WScript.Shell" )
WshShell.Run "C:\test.vbs", 1, True
Set WshShell = Nothing
End Sub

-->

 
Just to cover the bases, you aren't asking how to include an external script file are you?

a-script.vbs
Code:
Sub btnHello_onclick()
  MsgBox "Hello!"
End Sub
page.htm
Code:
<html>
  <head>
    <script type="text/vbscript" src="a-script.vbs">
    </script>
  </head>
  <body>
    <input type=button id=btnHello value="Click Me">
  </body>
</html>
The other examples given are really "how to have a web page run WSH to process a WSH script.
 
I am trying to run an external script from the html file.
Is this not possible?
The file i want to run (test.vbs) is a script that collectss information on the computer its connected to.
I want this file to run when a user clicks a button on a html file.
The code you gave works perfectly for your example, but my .vbs file contains no code similar to:
Sub btnHello_onclick()
Do you know of a way for me to achieve this?
 
i have tried the following in html to run the script:
<html>
<head>
<title>test</title>
</head>
<script language="vbscript">
strDel = "test.vbs"

dim scriptname
Public Sub RunScript(scriptname)
Set objWSH = CreateObject("WScript.Shell")
strCMD = "cmd /c cscript.exe C:\"& scriptname
objWSH.Run(strCMD)
Set objWSH = Nothing
End Sub

</script>
<body>
<h1>test</h1>

<INPUT TYPE=BUTTON OnClick="RunScript strDel" VALUE="RUN">This button should run test.vbs<br>
</body>
</html>

The cmd shell opens momentarily and then closes.
The script never executes.
 
Sounds like you want to spawn a WSH script from within an HTML page.

Warning: Free (worth the price) opinions ahead. ;-)

There are multiple reasons not to do this, almost all having to do with security, and others having to do with inconvenience. The latter is mostly due to the multiple security alerts your users should be getting when pages like this load and when they attempt actions that will create "hazardous" objects like WSHShell.

The cleanest thing to do may be to create your page as an HTA, and for the most part this is as easy as naming the file xxx.hta instead of xxx.htm. Unless the script runs a long time, you'd probably be better off just putting all of the grunt work in the HTA and forego running WSH at all.

There are times when you want special facilities of WSH though, some as mundane as the .Sleep() method.

Lets just go ahead with the plan you had.


Here is an example WSH script.

test.vbs
Code:
Option Explicit
Dim FSO, Args, tsOut

Set FSO = CreateObject("Scripting.FileSystemObject")
Set Args = WScript.Arguments
Set tsOut = FSO.CreateTextFile(Args(0) & "test.txt", True)
tsOut.WriteLine CStr(Now)
tsOut.Close
Set tsOut = Nothing
Set Args = Nothing
Set FSO = Nothing

'Detect script host used, notify the user.
If InStr(1, WScript.FullName, "cscript.exe", vbTextCompare) > 0 Then
  WScript.StdOut.WriteLine _
    WScript.ScriptName & " done, please press enter."
  WScript.StdIn.ReadLine
Else
  MsgBox "Done.", vbOkOnly, WScript.ScriptName
End If

This script wants one command-line parameter, the path to create a small test.txt file in. It is written to work via either CScript or WScript. If you don't need the pause at the end you can cut that stuff right out.

Here is an HTML page from which the WSH script can be kicked off using either CScript or WScript.

text.htm
Code:
<html>
  <head>
    <script language=vbscript>
      Option Explicit
      Dim WSHShell
      Dim Path

      Sub SetPath()
        Dim PathParts, X

        PathParts = Split(Mid(window.location.pathname, 2), "%")
        For X = 1 To UBound(PathParts)
          PathParts(X) = _
              Chr(CInt("&H" & Left(PathParts(X), 2))) _
            & Mid(PathParts(X), 3)
        Next
        Path = Join(PathParts, "")
        Path = Left(Path, InStrRev(Path, "\"))
      End Sub

      Sub btnCScript_onclick()
        'Run: cscript "{path}test.vbs" "{path}"
        WSHShell.Run _
          "cscript """ & Path & "test.vbs"" """ & Path & """"
        window.status = "Script has been run via CScript"
      End Sub

      Sub btnWScript_onclick()
        'Run: wscript "{path}test.vbs" "{path}"
        WSHShell.Run _
          "wscript """ & Path & "test.vbs"" """ & Path & """"
        window.status = "Script has been run via WScript"
      End Sub

      Sub window_onload()
        Set WSHShell = CreateObject("WScript.Shell")
        SetPath
      End Sub

      Sub window_onunload()
        Set WSHShell = Nothing
      End Sub
    </script>
  </head>
  <body>
    <input type=button id=btnCScript value="Use CScript">
    <input type=button id=btnWScript value="Use WScript">
  </body>
</html>
To keep things neat, I have it all in one folder. This includes the test.htm, the test.vbs, and even the test.txt that is created by test.vbs when it runs.

That is accomplished by having the HTML page fetch and decode its full path and source file and trim it back to just the path. This path is used both to run the WSH script and passed to the WSH script as an argument so that the WSH script knows where I want it to create test.txt for me. In your application you may not need to fuss with the path this way.


Why people mess about with running a command shell, wrapped around CScript, wrapped around their scripts sometimes escapes me. Once again there are good reasons to do this at times, but it usually isn't necessary.

If you think my code above is "full of quotes" it gets even worse if you run cmd.exe and have to feed it lots of stuff, like a complex command line. This may be where your attempt failed: not enough quotes.

Also, in most cases it is perfectly fine and even superior to use WScript instead of CScript to run small user-initiated utility scripts. If your WSH script doesn't do any user interaction, you never see WScript running anyway. If it does interact, using CScript you usually end up clunking around to try to keep the window open like I do in this example - or else invoke a command shell as you were doing above. The clunk-o-rama is usually needed because if the WSH script fails on some error, the "pause" I coded in mine above won't help. The console window just closes after a blink. Using WScript results in a nice error dialog you can read and then "ok."

That may be what was happening to yours.

The WScript MsgBox() and InputBox() functions are perfectly good for user interaction. If you need richer interaction you are probably better off writing the whole thing as an HTA in the first place.
 
isnt this wrong??

strDel = "test.vbs"

dim scriptname
Public Sub RunScript(scriptname)
Set objWSH = CreateObject("WScript.Shell")
strCMD = "cmd /c cscript.exe C:\"& scriptname
objWSH.Run(strCMD)
Set objWSH = Nothing
End Sub

the line
strCMD = "cmd /c cscript.exe C:\"& scriptname
should it read
strCMD = "cmd /c cscript.exe C:\"& strDel

 
strDel was passed to RunScript as a parameter (scriptname).

The [tt]Public[/tt] qualifier isn't needed, [tt]dim scriptname[/tt] is a redundant declaration, there should be a space before the [tt]&[/tt] concatenating scriptname, and no parentheses after the [tt]objWSH.Run[/tt].

All that said though, lauraSatellite's HTML and script do work. My guess would be that the "external" script being run in a command window terminates quickly or fails quickly - either one resulting in a rapid window-close. Thus the "blink."

Unless the script does console I/O, I'd change the CScript to WScript for a better look at what's happening if a script error is suspected.
 
hmm i must have had my eyes shut when i looked over that code!! dooh
 
The code works :) yay
Thanks a million to all those that offered comments.
Dilettante you were more than helpful.



<html>
<head>
<title>test</title>
</head>
<script language="vbscript">

Public Sub RunScript()
Set objWSH = CreateObject("WScript.Shell")
strCMD = "cscript.exe c:\test.vbs"
objWSH.Run(strCMD)

End Sub

</script>
<body>
<h1>testing</h1>

<h1>testing things</h1>

<br>
<INPUT TYPE=BUTTON OnClick="RunScript" VALUE="RUN">This is some text<br>

<br>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top