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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML / HTA & external VBScript functions

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi,

I'm writing a little HTA application to perform some common tasks on our application servers with HTML forms and VBScipt.

I'd like to have one VBScript file that containing all of my functions that are called in the HTML file. Would this also be know as server-side scripting?

All though googling reveals several hits for java script I haven't found anything pertaining to VBScript.

I guess the first question is, does anybody know if this is possible or is it a limitation of VBScript?


Bellow is the test html file.
In this example I am trying to call my function (sngMetric) using onClick= and passing the function the contents of a text box (txtText1). I have seen this done with javaScript.
Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</HEAD>
<script language="VBScript" src="/bin/test.vbs" type="text/VBScript"></script>

<BODY BGCOLOR="white">
     Input Inches: <INPUT TYPE="text" NAME="txtText1">
    <INPUT TYPE="button" NAME="cmdButton1" VALUE="Show Millimeters" onClick="sngMetric(txtText1.value)">
</BODY>
</HTML>
and here is the vbs file
Code:
Function sngMetric(strInches) 
   Dim sngInches
   sngInches = StrInches
   sngMetric = sngInches * 25.4
End Function

Any help would be greatly apreciated.

TIA
 
You should be able to do what you are trying. Are you getting an error? If you aren't getting an error and just want to see the value pop-up try the following:

Code:
<INPUT TYPE="button" NAME="cmdButton1" VALUE="Show Millimeters" onClick="alert sngMetric(txtText1.value)">

This is still considered client-side scripting.



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Hi dm4ever

Thanks v. much for your reply. Ok know I know it's possible the battle continues :)

Yes I get an popup error message:

"A Runtime Error has occurred.
do you wish to Debug?

Line: 10
Error: Type mismatch: 'sngMetric'"

I have taken the line "sngMetric(txtText1.Value)" and added it to the onClick="..." html statement.

The original HTML file looks like:

Code:
<HTML>
 <HEAD>
  <SCRIPT LANGUAGE="vbscript">
  <!--
   
    Sub cmdButton1_OnClick
     Dim strImperial
     MsgBox sngMetric(txtText1.Value)
    End Sub
 
    Function sngMetric(strInches) 
     Dim sngInches
     sngInches = StrInches
     sngMetric = sngInches * 25.4
    End Function
  -->
  </SCRIPT>
 </HEAD>
 
 <BODY BGCOLOR="white">
    Input Inches: <INPUT TYPE="text" NAME="txtText1">
    <INPUT TYPE="button" NAME="cmdButton1" VALUE="Show Meters">
 </BODY>
</HTML>

Any ideas what I am doing wrong.

Many thanks
 
Are you putting this in a .htm or .hta file? The html code with the vbscript in it works just fine on my machine. Do you get the error:


"A Runtime Error has occurred.
do you wish to Debug?
Line: 10
Error: Type mismatch: 'sngMetric'"


when trying to call the function in the example above or using the first method you mentioned or both?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Hi

The main page is an hta with an IFRAME linking to htm file.

If I run the html file with the vbscipt function and subs it works for me too, however, to get an external .vbs file I put:

Function sngMetric(strInches)
Dim sngInches
sngInches = StrInches
sngMetric = sngInches * 25.4
End Function [/color blue]

into test.vbs and called it from test.htm using:

<INPUT TYPE="button" NAME="cmdButton1" VALUE="Show Millimeters" onClick="sngMetric(txtText1.value)"> [/color blue]

Clicking on this button gives me the aforementioned error.

Thanks again.
 
The following worked for me on 3 pc's. Maybe someone else may have an idea of why it would generate an error for you.

HTML File: test.htm
Code:
<html>
<head>
<script language="vbscript" src="test.vbs"></script>
</head>
<body>
Input Inches: <INPUT TYPE="text" NAME="txtText1">
<INPUT TYPE="button" NAME="cmdButton1" VALUE="Show Meters" onclick="alert sngMetric(txtText1.Value)">
</body>
</html>

VBScript File: test.vbs
Code:
Function sngMetric(strInches)
	Dim sngInches
	sngInches = StrInches
	sngMetric = sngInches * 25.4
End Function

What if you first try seeing if a value can be passed to the function defined in the .vbs without getting the value from the text field?

i.e <input type="button" name="cmdbutton1" value="Show Meters" onclick="alert sngMetric('2')">

If this works (showing a popup with a value of 50.8), then the problem is probably retrieving the value from the input box.


--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
[1] The onclick handler written like this:
>onClick="sngMetric(txtText1.value)">
serve no purpose even if it executes correctly, does it change anything of the world?

This is minimum changes demonstrating the working.
[2] Give the iframe a name "iframename".
[3] Add something someplace to show the result.
[tt]
Input Inches: <INPUT TYPE="text" NAME="txtText1"><br />
equiv Metric: <INPUT TYPE="text" NAME="txtText2" readonly="readonly" style="border:none;"><br />
<INPUT TYPE="button" NAME="cmdButton1" VALUE="Show Millimeters" onClick="txtText2.value=window.frames('iframename').sngMetric(txtText1.value)">
[/tt]
Personal Note (I seldom put): I am not impressed the way controls are referred in the way op is using so much that I would not do the referencing that way at all. Just to demonstrate how to do it the way op's comprehend.
 
Try forcing a type conversion
Code:
Function sngMetric(strInches)
    Dim sngInches
    sngInches = [COLOR=blue]cdbl([/color]StrInches)
    sngMetric = sngInches * 25.4
End Function
 
Fixed it! It's amazing what a good night's sleep can do :)

For reference the code that was wrong was in the test.htm file. The code in the inset works. Of course the original inline code sub _onClick had a message box. This code didn't have anywhere to send the output. I see now dm4ever's suggestion of the onclick=" alert ... (which I am sure I tried earlier with no luck. grr!)

The actual culprit was one little '\' in the src referencing "\bin\test.vbs". Should be "bin\test.vbs". I'll blame dreamweaver for this error :)

I tried dm3ever's suggestion of passing a value directly to the function. Nothing. Which made me realise it needed somewhere to put the result. (alert or msgbox)

xwb: Thanks but forcing it as a double didn't make any difference.

tsuji: Thanks but I didn't need this. In this test there are no iFrames envloved. i will bare in mind for the main project. Does the IFRAME need to be referenced even if all code is running in the same html file?

Not sure what you mean by:
I am not impressed the way controls are referred in the way op is using so much that I would not do the referencing that way at all. Just to demonstrate how to do it the way op's comprehend.
What's "op" ?

Anyway. The working code:
Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</HEAD>
<script language="VBScript" src="test.vbs" type="text/VBScript"></script>

<BODY BGCOLOR="white">
     Input Inches: <INPUT TYPE="text" NAME="txtText1">
    <INPUT TYPE="button" NAME="cmdButton1" VALUE="Show Millimeters" onClick="msgbox sngMetric(txtText1.value)">
    
 </BODY>
</HTML>

Anyway thanks very much for everyone's assistance on this. Is this forum like a particular "paid" forum where one awards points?

Many thanks.
 
You are now not referring to vbscript src loaded in an iframe page. So whatever you say work is meaningless to the original question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top