Persits Software, Inc. Knowledge Base Articles

Using JavaScript instead of VB Script

Problem Description

All code samples shipped with the Persits Software components are written in server-side VB Script. There are no server-side JavaScript (JScript) code samples. Certain VBScript constructs, such as For/Each and On Error Resume Next, do not have direct equivalents in JavaScript, so caution must be taken when converting VBScript into JavaScript.

Solution

The following boxes show a few typical code samples written in VBScript, and their JavaScript equivalents.

1. AspUpload - setting properties and scrolling through the Files collection

In VB and VBScript, you can use the special loop statement For-Each to iterate through elements of a collection. JavaScript lacks such a construct, so a regular for loop must be used.

Note that JavaScript is case-sensitive. Also, every line in JavaScript must be terminated by a semicolon and parentheses are required for all method calls.

VB Script JavaScript
Dim Upload, File, Count, i

Set Upload = Server.CreateObject("Persits.Upload")
Upload.OverwriteFiles = True

Count = Upload.Save("c:\upload")

For Each File in Upload.Files
   Response.Write File.Path + "<BR>"
Next

Response.Write Upload.Form("Description")

var Upload, File, Count, i;

Upload = Server.CreateObject("Persits.Upload");
Upload.OverwriteFiles = true;

Count = Upload.Save("c:\\upload");

for(i = 1; i <= Upload.Files.Count; i++ )
{
   File = Upload.Files(i);
   Response.Write( File.Path + "<BR>" );
}

Response.Write( Upload.Form("Description"));

2. AspUpload - Catching exceptions

There is no On Error Resume Next statement in JavaScript, so you must use the try/catch construct instead. Details of an error can be obtained via the err object much the same way as via the Err object in VBScript.

Note that the Java Script implementation of the err.number property is different from that of VBScript's Err.Number in that the former returns a double-word hexadecimal error code such as 0x800A0008, and the latter only returns the low-order word such as 8.

VB Script JavaScript
Dim Upload, File, Count, i

Set Upload = Server.CreateObject("Persits.Upload")
Upload.SetMaxSize 4000, True

On Error Resume Next
Count = Upload.Save("c:\upload")

If Err.Number = 8 Then
   Response.Write "File too large."
End If

var Upload, File, Count, i;

Upload = Server.CreateObject("Persits.Upload");
Upload.SetMaxSize(4000, true);

try
{    Count = Upload.Save("c:\\upload");
}
catch(err)
{
   if(( err.number & 0xFF ) == 8 )
   {
      Response.Write( "File too large.");
   }
}