JavaScript
  Home arrow JavaScript arrow Useful Web Widgets
Moblin
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Useful Web Widgets
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-01-18

    Table of Contents:
  • Useful Web Widgets
  • Getting Down With Father Time
  • The Function in Depth
  • A Calendar Engine
  • The Interface Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    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.

    More JavaScript Articles
    More By Chris Root


       · Thank you for reading the article. Comments are welcome.
     

    JAVASCRIPT ARTICLES

    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway