How to Create a Simple VBScript AJAX Application

A Quick Tutorial Showing on Working With AJAX and VBScript

© Mark Alexander Bain

Jul 28, 2009
How to Create a Simple VBScript AJAX Application, Mark Alexander Bain
A VBScript developer need not be daunted by the thought of starting to use AJAX. In fact nothing could be easier than creating a VBScript based AJAX application.

Any VBScript programmer considering working with AJAX does not need to worry about learning a new programming language. All they have to remember is that AJAX (which stands for Asynchronous Javascript and XML) is a simple technique that:

  • sends a request for information from a web page to a server
  • dynamically updates elements in the web page with the information returned by the server

And therefore the VBScript just needs to:

  • create a VBScript CGI web page
  • create a Javascript web page that will obtain the required information from the CGI web page

Neither of which are difficult.

The VBScript Element of the AJAX Application

In this example the Javascript element of the AJAX application will send an asynchronous request from the user's computer to the VBScript element on the web server. The VBScript element is actually a CGI web page as discussed in <a href="http://windows-programming.suite101.com/article.cfm/professional_cgi_web_development_with_vbscript">Professional CGI Web Development with VBScript</a>. The article explains that a VBScript header file is required. This header file can:

  • process any inputs
  • run the commands in the VBScript CGI file

In this example the header will look like:

Set fso = CreateObject("Scripting.FileSystemObject")
Set original = FSO.OpenTextFile(WScript.Arguments(0))
firstline = original.ReadLine
ExecuteGlobal original.ReadAll

And, if it saved in the cgi-bin directory then the first two lines of the VBScript CGI file will be:

#!c:\WINDOWS\system32\cscript.exe /nologo "C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\header.vbs"
wscript.echo "Content-type: text/html" & vbcrlf

These are:

  • the shebang line - this tells the web server where to find the script interpreter
  • the HTML header line - this must be the first output from any CGI page

The VBScript element of the AJAX application does not have to to anything spectacular (yet). All it has to do is return an output, something as simple as:

wscript.echo time

If this is saved into the web server's cgi-bin directory as "simple_ajax.vbs" then it can be tested by viewing it in a web browser (as shown in figure 1 at the bottom of this article). If that works correctly then the VBScript CGI is ready for AJAX.

The Javascript Element of the AJAX Application

With the VBScript file in place on the server, the programmer now needs to create a web page. This will use Javascript to:

  • create an XML request object
  • send the request to the server
  • update the document with the information obtained

The code starts by creating the XML request object. As it does it takes account of the browser type being used:

<head>
<script type = "text/javascript">
var XMLHttp;
if(navigator.appName == "Microsoft Internet Explorer") {
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
XMLHttp = new XMLHttpRequest();
}

Next a function is required. This will:

  • open a connection to the CGI page
  • send the request
  • monitor the status of the request and then update the document once the request has finished running:

The code to do this is:

function show_time () {
XMLHttp.open("GET", "/cgi-bin/simple_ajax.vbs", true);
XMLHttp.onreadystatechange = function() {
if (XMLHttp.readyState == 4) {
document.getElementById( 'time_div' ).innerHTML = XMLHttp.responseText;
}
}
XMLHttp.send(null);
}
</script> </head>

Finally, some HTML code is required that will:

  • define a button
  • define a div area

and so the body of the web page is very simple:

<body>
<input type = button onclick = "javascript: show_time ()" value = "Get Time">
<div id = "time_div">Click the button to see what time it is</div>
</body>

If this is saved to the web server (as "simple_ajax.html", for example) then the CGI application is ready for use.

Running the VBScript AJAX Application

Once the web page has been saved on to the web server (into the htdocs directory) then it can accessed by using a web browser. The user will not see the VBScript code being run, and all that they will see is portions of the web page being updated (as can be seen in figure 2).

So, now the VBScript programmer can make the most of developing any AJAX applications that is required of them, and they can do it with the confidence that they already have all of the necessary skills at their finger tips.


The copyright of the article How to Create a Simple VBScript AJAX Application in AJAX Programming is owned by Mark Alexander Bain. Permission to republish How to Create a Simple VBScript AJAX Application in print or online must be granted by the author in writing.


How to Create a Simple VBScript AJAX Application, Mark Alexander Bain
Figure 1: A Simple VBScript CGI Web Page, Mark Alexander Bain
Figure 2: A Simple VBScript AJAX Application, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo