Keith Joubert logo - home.

Contact

Binary Search
Sort Multiple Columns
Filing Functions
Extracting Excel data
Read and Write .csv files
Show Progress Messages
Send Automatic Email
Running under .hta
Setting Folder Attributes
Long-script warnings
Desktop Shortcut
ADODB & SQL queries


 

 
JAVASCRIPT TIPS

While writing programs, I have often searched the Web for answers to particular problems, and have been grateful for the assistance gleaned from many sources. 

In the interest of returning the favour/s, this small group of pages has been written.  I hope that the contents will be of interest and that they will benefit other programmers searching for an answer.

Binary search becomes very useful when large arrays are used.  The developed Binary Search algorithm handles mixed numerical-alphanumerical array data - a simple array or an array of arrays.  It optionally returns the first instance of a target value, permitting one to iterate up through the other instances of that value.

For the binary search, a sorted array is necessary.  JavaScript has an efficient built-in algorithm in its sort() function.  However, this only sorts alphabetically.  Therefore, an additional, purpose-built function is required to handle particular data types.  This Binary search algorithm is designed to search through an array previously sorted by the SortIt() function described below.

The SortIt() function uses the JavaScript sort() function to sort multiple columns in numerical/alphabetic order, with numerals first.  In a recent application, arrays involving up to 10 columns of data also had to be sorted.  In this case, the greatest required depth turned out to be a sort on 6 columns.  SortIt() does this pretty quickly.

Filing is not native to JavaScript.  However, in desktop applications, ActiveX can be used in conjuction with JavaScript.  All the normal file operations, reading/writing/appending data, creating folders, setting attributes, etc can be done.

Extracting data from Excel .xls files and putting it into arrays can be done quite neatly with JavaScript and ActiveX, in conjunction with Excel, working in the background, invisibly. The GetXLS function shows how to read mutiple files and sheets into a group of .csv files.

Reading and writing  .csv files into/from arrays is conveniently carried out by two dedicated functions - see Read and Write .csv files

It is often desirable to display a message in a form-field, informing the user of the processing being carried out  by functions that involve considerable processing time.  During processing, the browser does not display such messages.  To overcome this, the JavaScript function setTimeout  can be used to allow the display.  in a fairly complex program, with several long-running functions, the interleaving of the timing calls can be less than obvious.  The examples shown under ProgressMessages should clarify a workable approach.

During processing of incoming data, usually from others, you may need to error-trap and validate the data, issuing an Error-Report to the originator.  A convenient way to do this is to send an email automatically.  Using ActiveX and Outlook, the AutoMail function fulfills this.

To avoid the operating system ActiveX warnings, your program should run under an .hta envelope, ie, as an application (like an .exe file).  Details of how to do this are given.

IE issues warnings regarding the length of a running script.  When handling complex calculations/disk operations, etc, these warnings become a nuisance and detract from usability, adding operator annoyance.    To avoid continuous long-script reminders, one needs to "tune" the registry.  This is quite simple.

A desktop shortcut icon to startup your application is useful and gives a nice finishing touch.

You may encouter a lot of difficult-to-trace and varying error reports when running on Windows XP and Server - faults that did not arise when running on your development system.  This can be due to idiosyncrasies of the MS system, and not due to your programming.  It was found that the Read-only Attribute can not be permanently changed by Windows Explorer.  The attribute is reset to read-only after one iteration.  Thus a write operation would result in an error, the second and subsequent time a write or delete was attempted.  A command-line or JavaScript folder attributes action is required.

You may wish to retrieve data from a Microsoft SQL database, using Javascript.  The use of ADODB, ActiveX, Streaming, and RecordSets can enable useful functions to be performed.  The principles would also apply to writing data to an SQL database.  Some typical function code is shown: ADODB & SQL queries.

I hope these pages prove useful!   Please hi-light the portions of text you wish to retain, and use 'Copy' or ctrl+C.

TopTop