Products
Services
Contact Us

Scripts: Javascript :: Cookies :: Library Article #15

Developer's Section

Using Cookies in Javascript
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (2441)
Favorites:

Learn how to use cookies using javascript.

One of the most common ways to store data used by web applications in your computer is by using Cookies. They are used by many websites and can have a lot of benefits. Most sites will use cookies to remember information they have previously collected about the users activities, preferences, and login information.

In the following code snippets we will take a look at the most common methods to create, update, and check for a cookie.

Here is how to set up a cookie:

 Code Snippet 1

<script type="text/javascript">
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.toGMTString());
}
</script>


Here is how to get a cookie:

 Code Snippet 2

<script type="text/javascript">
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 "";
}
</script>


Deleting Cookies:
the easiest way to delete a cookie is to reset the cookie expiration date to 0 days.

 Code Snippet 3

<script type="text/javascript">
//delete a cookie by setting the expiration days to 0
setCookie("the cookie name i want deleted","",0);
</script>


See other Scripts in Cookies

 

Submit Your Scripts:


System Message:
  • Your Javascript script has been sent. Please allow 48 hours for processing.


If you would like to have your Javascripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]