Useful Web Widgets
(Page 1 of 5 )
If you do a lot of web programming, it can save you a lot of time and effort to have a library of JavaScript functions. This article presents some useful, reusable client side code, for calendars and more.
When you are starting a project it can be pretty nice having some pre-written code to work with to make things easier. You may have built up an arsenal of PHP, JSP or C# classes, a few Perl scripts and other sundry tools, but do you have any good reusable client side code?
Putting together a library of JavaScript functions that can be pulled out and used any time you need them can prove pretty useful. Though JavaScript is not object oriented in the same way that many other languages are, with proper coding it can be organized into reusable functions and objects. In this article we will look at three JavaScript utility functions that you can add to your code arsenal.
In fact these functions are not exclusively DOM related and could be used in other ECMA Script environments such as Actionscript, CFScrript and jscript.net with little or no modification. When developing actionscript projects I often develop them in JavaScript first. I find a web browser environment to be easier to work with when developing simpler non user interface related functions.
Generating Random Identifiers
Coming up with hard to guess passwords and other identifiers that are unique can be difficult. It can be harder still to remember all of them. Though as a web developer you can't do much about improving a user's memory, you can certainly help keep your system secure by providing good randomly generated passwords, order numbers, and other identifiers. This script described in this section can also be used to produce identifiers for database records and anything else for which you may need a unique identifier.
This function uses a two dimensional array which contains characters for use in our identifier. It offers the flexibility of producing a string of characters as long or short as you need. Eight characters is the most common length, but it can produce strings that are much longer.
function id_gen()
{
var vals = new Array()
vals[0] = new Array
(0,2,8,"k",5,"x","f","s","e","t","u",4,"g","c","k");
vals[1] = new Array
("p","n","b",5,"h",8,"z",9,"n","x","v");
vals[2] = new Array
("z","0","e","1","f",7,"w","v","n","s",1,"m","r");
vals[3] = new Array
("q",5,"b",4,6,7,8,0,"a","w",9,"y","m",2,3);
vals[4] = new Array
(7,8,0,"a","q","w","y",5,"h",8,"z",9);
vals[5] = new Array
("e","t","u",4,"g",9,7,8,0,"s","d","j",5,"x");
vals[6] = new Array
("!","z","g","n","w",9,4,7,"p",0,"a","q","w");
In the first part of this function we set up a multidimensional array. Each of the elements in the "vals" array is an array of characters, both numbers and letters. When deciding what characters to use, try to make them as randomly distributed as possible. Avoid using the letter "O;" use zeros instead. You may or may not want to mix capital and lowercase letters and include special characters such as "!","@" or "#", it's up to you. The goal here is to have as many possibilities as possible. This array can be as large as you want it to be, but seven elements seems to provide enough variation for most uses.
We then need to initialize a variable for our character string, set a character string length and determine the length of the "vals" array. A for loop runs for the specified number of times, putting a randomly selected character in the character string. Each time around a random number between 0 and the number of the last element in the "vals" array is produced. This number is used to select which of the character arrays to grab a character from. A second random number is then produced which has a value between 0 and the number of the last element in the character array that was selected. That second random number is then used to retrieve a value from the selected character array. This character is then added to our character string and once we have reached the desired number of characters, the character string is sent out of the function.
var str = "";
var stringLength = 9;
var len = vals.length - 1;
var num;
for(var i = 0;i < stringLength;i++)
{
num = Math.random()*(len);
num = Math.round(1+num);
num = num - 1;
var num2 = Math.random()*((vals
[num].length-1)-1);
num2 = Math.round(1+num2);
str += vals[num][num2];
}
return str;
If you wanted to make this function more configurable from the outside you could easily set the length of the character string using a function argument as well as include or exclude elements from the main array using an argument.
function gen_id(specialCharacters,stringLength)
{
var vals = new Array();
vals[0] = new Array
(0,2,8,"k",5,"x","f","s","e","t","u",4,"g","c","k");
vals[1] = new Array
("p","n","b",5,"h",8,"z",9,"n","x","v");
vals[2] = new Array
("z","0","e","1","f",7,"w","v","n","s",1,"m","r");
vals[3] = new Array
("q",5,"b",4,6,7,8,0,"a","w",9,"y","m",2,3);
vals[4] = new Array
(7,8,0,"a","q","w","y",5,"h",8,"z",9);
vals[5] = new Array
("e","t","u",4,"g",9,7,8,0,"s","d","j",5,"x");
if(specialCharacters)
{
vals[6] = new Array("!","@","$","#");
}
var str = "";
if(stringLength == 0 || stringLength == "")
{
stringLength = 9;
}
One way this function could be used, would be to fill a form field with a default value. If the user had a better idea for a identifier they could simply change it.
Next: Getting Down With Father Time >>
More JavaScript Articles
More By Chris Root