Gmail / Facebook Style Chat Application
Details Live Demo Download


Posted: Friday 16th of 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.

Cookies are a technology which can be easily and simply used by a Webmaster to achieve a great and many useful tasks when creating websites.
Although cookies are well known to users, many people are not really sure what they are used for, and a large amount of webmasters don't realize the possibilities open to them when they use cookies.
Others have been put off, thinking that they must be difficult to use, but in reality, cookies can be set and used by a simple command in most scripting languages. In this tutorial I'll cover setting, reading and deleting cookies in JavaScript.

For a more detailed information on cookies, please Read This

Please download the script by clicking on the big download button if you like it because you may get errors if you directly copy the code from this page.


JavaScript Code:

<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>






That's it guys...



Comments
0

 

Announcement x
Dear Users and Clients,

This is to inform you that we have recently updated our website generally.

If you experience any problem viewing any part of this website or if you notice that any of our scripts or tutorials are not functioning properly as should be, please click on the Contact Link at the top of this page to report the issue to us.

Our priority is to give you the best of service!


Thank You,
The Vasplus Team

Announcement