Friday 16 August 2013

How to Set, Read and Delete Cookie using JavaScript.

This tutorial helps and teaches you how to Set, Read and Delete Cookie using JavaScript.

What is a cookie?

A cookie is simply a variable that your website page can store on or retrieve from the user's computer.

The idea is that the next time the user arrives at your website page, the value of the cookie can be read by the website page, and then used for a desired purpose.

<script type="text/javascript">
  
// Function to set a Cookie function vpb_set_cookie(cookie_namecookie_valuetotal_days_allowed)  
{     
    if (total_days_allowed != "")      
    {         
       var vpb_date = new Date();
          vpb_date.setTime(vpb_date.getTime() + (total_days_allowed*24*60*60*1000));
 
        var vpb_expires ";  expires="+vpb_date.toGMTString();     
     }     
     else { var vpb_expires ""}                

document.cookie cookie_name+"="+cookie_value+vpb_expires+";  path=/";

// Function to read a Cookie 
function vpb_read_cookie(cookie_name)  { 

    var vpb_cookie_name cookie_name "=";     var split_cookie_name document.cookie.split('; ');     
    for( var 0;  split_cookie_name.length;  i++ )      
    {
        var vpb_split_cookie_name split_cookie_name[i];         

       while ( vpb_split_cookie_name.charAt(0)==' ' )         
        {             vpb_split_cookie_name vpb_split_cookie_name.substring(1,vpb_split_cookie_name.length);             
             if ( vpb_split_cookie_name.indexOf(vpb_cookie_name) == )        

return vpb_split_cookie_name.substring(vpb_cookie_name.length,vpb_split_cookie_name.length);         
              }     
          }     
          return null

// Function to delete a Cookie 
function vpb_delete_cookie(cookie_name)  {     

    vpb_set_cookie(cookie_name"", -1); 

//This is how you should use the above function to set the cookie with 10 days to keep the cookie active before it expires. vpb_set_cookie('fullname','Victor Olu',10); 

//This is how you should use the above function to delete your cookie - The cookie will be deleted if you uncomment the below code. 
//vpb_delete_cookie('fullname'); 

//This is how you should use the above function to read or get your cookie after setting it. 
alertvpb_read_cookie('fullname') ); 
</script>

Click here to view this tutorial

3 comments:

  1. One from the latest trends in carpeting may be
    the frieze because in the resiliency of the fibers.
    "One in our most exciting collections is our WCMA (Window Covering Manufacturers Assn. Among cleaning machines, steam cleaners are somewhat unique. Laminate Flooring is an superior form of flooring that's accessible in distinctive kinds. Limit the quantity of mites living in for the bed by within the mattress and pillows having a plastic cover.

    My webpage: best vacuum for hardwood floors

    ReplyDelete
  2. Although this sort of flooring is definitely an ideal variety for the home, it can be important to
    take care of the hardwood in order to maintain it looking as
    nice as is possible. Again, the main floor installment specialist ought to be
    able to offer this information. If a no-shoes policy doesn't seem likely to your friends and family, have walk-off
    mats on both sides in the door so people can thoroughly wipe
    their feet before entering your house. A dresser supplies
    a place that will put folded clothes, and a bedside table gives a place for your alarm clock along with a lamp.
    For one, even the vacuum works quite effectively on tough floors,
    it turned out less effective on thick carpets because of its light weight.



    Also visit my website what is the best dyson vacuum for hardwood floors

    ReplyDelete
  3. It's perfect time to make some plans for the
    future and it's time to be happy. I've read this
    post and if I could I wish to suggest you some
    interesting things or advice. Perhaps you can write next articles referring to this article.
    I want to read more things about it!

    Take a look at my blog: reklama zewnetrzna lubin

    ReplyDelete