Wednesday, September 8, 2010

JavaScript Trim Functions

A JavaScript string function does not have in built string trimming functions available. Hence we found following codes when we Google it out on the net. To save your time in searching for required string trimming functions we consolidated all the information we got from the net.

The first is a definition of the seperate function ltrim, rtrim and trim. These trim the spaces to the left of the string, the right of the string and both sides respectively.

function trim(s)

{

return rtrim(ltrim(s));

}

function ltrim(s)

{

var l=0;

while(l <>

{ l++; }

return s.substring(l, s.length);

}

function rtrim(s)

{

var r=s.length -1;

while(r > 0 && s[r] == ' ')

{ r-=1; }

return s.substring(0, r+1);

}

If you are only going to use trim, you might want to use this, probably faster, defenition:

function trim(s)

{

var l=0; var r=s.length -1;

while(l <>

{ l++; }

while(r > l && s[r] == ' ')

{ r-=1; }

return s.substring(l, r+1);

}

Reference Blog: http://doc.infosnel.nl/javascript_trim.html


If you are familiar with REGEX following code will provide you more better efficiency than previous codes. This will also reduce your lines of code.

function trim(stringToTrim) {

return stringToTrim.replace(/^\s+|\s+$/g,"");

}

function ltrim(stringToTrim) {

return stringToTrim.replace(/^\s+/,"");

}

function rtrim(stringToTrim) {

return stringToTrim.replace(/\s+$/,"");

}

// example of using trim, ltrim, and rtrim

var myString = " hello my name is ";

alert("*"+trim(myString)+"*");

alert("*"+ltrim(myString)+"*");

alert("*"+rtrim(myString)+"*");

Reference Blog: http://www.somacon.com/p355.php

Always Search @ Fukat Ka Gyan: Hub For Quick Solutions

Monday, September 6, 2010

Dynamic Field Name in Custom List Form

A common problem that is normally faced while using Custom List forms or Data View Web Part is Column Name doesn’t get updated automatically in the customized form if any change is made to the Column name in the list.

For resolution of this problem use following code in place of hard coding column names in the custom list forms.

ControlMode will change based upon the form type (New, Edit and Display)

ControlMode=”New / Edit / Display”

FieldName=Internal name of the fields

References:

http://ankushg.spaces.live.com/blog/cns!12D87E23D9058350!235.entry

Always Search @ Fukat Ka Gyan: Hub For Quick Solutions

Friday, September 3, 2010

Working with Cookies

A cookie is a variable that is stored on the visitor's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With JavaScript, you can both create and retrieve cookie values.

If c_name is the name of the cookie.

Creating Cookies:
Use the following function to create a cookie,

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

Reading Cookie Value:
You can the get the values of the cookie created in the first step using the following function. You just need to pass cookie name to this function,

function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}

Deleting Cookies:
Use the following function to delete a cookie. Just pass the name to the function,

function Delete_Cookie(c_name)
{
document.parentWindow.parent.document.cookie = name + "=" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

Always Search @ Fukat Ka Gyan: Hub For Quick Solutions