This page has been translated using an automatic translation service and the translation may not be very accurate.

Javascript Library Base (English)

If you use this code and feel it is a useful tool, consider making a donation (through PayPal) to help support the project. You can donate as little or as much as you wish, any amount is greatly appreciated!

Italian page For the original Italian page, please click on the link: http://guru4.net/articoli/javascript-base-library/default.aspx

Never it is capitato, developing code to you client-side, of porvi questions like: "but as the dimensions of an element are estimated" or: "I must validate an address email but that one regular expression just not me the memory!" or still: "accidents, it would be better to eliminate the spaces begins them from this tightens". Beh, to me yes! And a lot often. Therefore every time I find myself to scartabellare between the old plans to the search of "that function that me memory of to have already written" or to carry out a search in Internet of that code that me seems face to the case mine. Tired with these peregrinations, I have put all the indispensable functions in beautiful rows Javascript: I hope can be useful also to you as it it is to me!

String: trim, lTrim and rTrim

In Javascript the lack of the functions is felt Trim(), LTrim() and RTrim() of VBScript in order to remove the spaces it begins them and/or final from one it tightens:

function trim()
{
    var newstr = this "";
    newstr = newstr.lTrim();
    newstr = newstr.rTrim();
    return newstr;
}
String.prototype.trim = trim;

function lTrim()
{
    var newstr = this "";
    while(newstr.charAt(0) == " ")
        newstr = newstr.substring(1, newstr.length);    
    return newstr;
}
String.prototype.lTrim = lTrim;

function rTrim()
{
    var newstr = this "";
    while(newstr.charAt(newstr.length - 1) == " ")
        newstr = newstr.substring(0, newstr.length - 1);    
    return newstr;
}
String.prototype.rTrim = rTrim;

String: replaceText

The method replace it is much powerful one, accepting one regular expression for the definition of the substitution criterion. Often but a simple method of substitution of characters would be sufficient:

function replaceText(pattern, substitute)
{
    return this.split(pattern).join(substitute);
}
String.prototype.replaceText = replaceText;

Verification of one given

In order to verify if the day values, month and year correspond to one given valid:

function isDate(year, month, day)
{
    var d = day "";
    var m = month "";
    var y = year "";
    if((d == "") || ( m == "") || ( y == ""))
        return false;
    if(isNaN(d) || isNaN(m) || isNaN(y))
        return false;
    if(((parseFloat(d) "") != (parseInt(d, 10) "")) || ((parseFloat(m) "") != (parseInt(m, 10) "")) || ((parseFloat(y) "") != (parseInt(y, 10) "")))
        return false;
    d = parseInt(d, 10);
    m = parseInt(m, 10);
    y = parseInt(y, 10);
    if(d < 1 || m < 1 || y < 1)
        return false;
    if(m < 1 || m > 12)
        return false;
    if((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) && (d > 31))
        return false;
    if((m == 4 || m == 6 || m == 9 || m == 11) && (d > 30))
        return false;
    if((m == 2) && (d > 29))
        return false;
    if((m == 2) && (!isLeapYear(y)) && (d == 29))
        return false;
    return true;
}
function isLeapYear(year) // Check leap year
{
    return ((year % 4 == 0 && year % 100 != 0) || ((year % 4 == 0 && year % 100 == 0) && (year % 400 == 0)));
}

Verification of an address email

In order to verify if one tightens it corresponds to a syntactically valid address email:

function isValidEmail(email)
{
var re = /^(([^<>()[\]\\.,;:\s@\"] (\.[^<>()[\]\\.,;:\s@\"] )*)|(\". \"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9] \.) [a-zA-Z]{2,}))$/
return re.test(email);
}

To obtain the position of an element: getElementPosition

The function as an example receives like parameter the reference to the object - gained by means of document.getElementById(object id) - and of it it gives back the position in pixel (top and left)

function getElementPosition(obj)
{
    var t = 0;
    var l = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            t = obj.offsetTop;
            l = obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.y && obj.x)
    {
        t = obj.y;
        l = obj.x;
    }
    return {left : l, top : t};
}

To obtain the dimensions of an element: getElementSize

The function as an example receives like parameter the reference to the object - gained by means of document.getElementById(object id) - and of it it gives back the dimensions in pixel (width and height)

function getElementSize(obj)
{
    var w = obj.offsetWidth;
    var h = obj.offsetHeight;
    return {width : w, height : h};
}