WebRef Update: Featured Article: Mix and Match: Using VBScript to define and control JavaScript on Active Server Pages
|
Mix and Match: Using VBScript to define and control JavaScript on Active Server Pages
In the course of my Web page work I often have used JavaScript and VBScript on Active Server Pages (ASP). While searching the Web for references to help solve programming problems, it is easy to find pages to give examples on using one or the other, but rarely do you see information regarding using them both together.
Using VBScript to control JavaScript can be as simple as using the VBScript to determine whether the JavaScript will be presented to the client browser at all. For instance, you may decide that unless a browser is at least compatible with Mozilla version 4, a given JavaScript function will not be written. In this example, we check the browser information sent by the client before writing our function.
<%strUA = Request.ServerVariables("HTTP_USER_AGENT") If InStr(strUA,"Mozilla/4") then%> Function fnMyNewFunction(){ document.write("This is not a test."); } <%End If%>Those of you familiar with VBScript know that the tags <% %> mark the beginning and end of a VBScript, the contents of which are interpreted at the time the page is presented to the browser. If the client browser is not Mozilla 4 compatible, then the fnMyNewFunction is never written, and the client browser never even sees that it exists.
I recently had a more difficult challenge where I was asked to create a bar chart that displayed customer account history. The constraints were 1.) spawn a new page, and 2.) use an existing object that had already been populated with data. Since the project was written specifically for a client, I can't give you the full code listing, but I can walk you through the logic of the development process.
I decided I would populate a JavaScript array with data from the existing VB object, then use that array to determine the height of my images in my bar chart. Then using JavaScript, I would display the chart in a new window. (For more on creating new windows see "Window Spawning and Remotes" - webreference.com/js/column7).
First, using JavaScript, I declare the array I want to populate:
var arryTotAmt = new Array;
Then, using a VBscript For-Next loop, I populate the array with the values from my VB object. In this example, my VB object is named "PaymentHistory". It has already been created, and populated with values from my database, and has two important properties: "Count", which contains the total number of records in the object, and "Item", which is the nth instance of a record. Our PaymentHistory object contains many different fields associated with customer accounts, but for the purposes of our graph we are only interested in a property called "Charge". The Charge property of my PaymentHistory object contains the actual monthly value I want to put into my JavaScript array.
<% for i = 1 to PaymentHistory.Count%> arryTotAmt[<%=i%>]=<%=Cstr(PaymentHistory.Item(i).Charge)%>; <%next%>Next: Another Example
This article originally appeared in the January 13, 2000 edition of the WebReference Update Newsletter.
Comments are
welcome
Written by Jerry Gray and
Revised: May 10, 2000
URL: https://webreference.com/new/vbscript.html