follow me on Twitter

    Subscribe to
    Posts [Atom]


    Student Planning Wizard

    Wednesday, October 11, 2006


    javascript trim function

    Monday, October 09, 2006

    function leftTrim(sString)
    {
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    return sString;
    }



    ------------------------------------


    function rightTrim(sString)
    {
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
    }


    -----------------------------------------------------------



    The allTrim() JavaScript function combines both leftTrim() and rightTrim() functions:

    function trimAll(sString)
    {
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;

    javascript string literal


    var aVAR ='"\'<%context%>\'"'


    for a literal string to be entered in use this example:

    method(\"'+string+'\")

    would produce: string

    to insert a variable:
    obj.setAttribute("onclick", "aFunction(" + i +")"

    getting the application context in javascript


    first you create a jsp page with the following content:
    <%@page contentType="text/javascript"%>
    var context ='"\'<%= request.getContextPath() %>\'"';

    note: the context will have a string with the quotes still around it so these will need to be trimmed

    then in the page or place javascript is included in your program include this file, for example:

    script type="text/javascript" src="ContextPath.jsp" (surrounded by script tag)

    the above context varialbe is now a global javascript variable

    internet explorer fix for setting javascript dom button onlick method


    setting the javascript dom button onlick method doesn't work in inter net explorer

    the solution is to set the elements onclick to a function and carrry out the function
    from the set function

    for example

    element.onclick = linkClick;

    function linkClick(){

    someFunction();
    }