Monday , 29 May 2017
Home » JavaScript » [JavaScript] How to Create/Store/Delete Cookie with JavaScript
[JavaScript] How to Create/Store/Delete Cookie with JavaScript

[JavaScript] How to Create/Store/Delete Cookie with JavaScript

Hello Friends,

I am working on WordPress shopping cart and i found that shopping cart is working with JavaScript Cookie. If you are PHP Developer than you might be aware that we are using session for shopping cart management. But we can also use cookie for shopping cart management. You might be aware with PHP Cookie but JavaScript Cookie is little bit different. Here i am showing you how to create JavaScript Cookie, How to Get Cookie Using Javascript, How to Delete JavaScript Cookie ?

Set Cookie using JavaScript:

function set_cookie(cookie_name,cookie_value,number_days)
{
var expiry_date=new Date();
expiry_date.setDate(expiry_date.getDate() + number_days);
var cookie_value=escape(cookie_value) + ((number_days==null) ? "" : "; expires="+expiry_date.toUTCString());
document.cookie=cookie_name + "=" + cookie_value;
}

Get Cookie using JavaScript:

function get_cookie(cookie_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==cookie_name)
{
return unescape(y);
}
}
}
      
Plugin by: PHP Freelancer

About Rakshit Patel

I am PHP Freelancer, Rakshit Patel working as PHP Programmer India. I have developed this site to share my knowledge, thoughts and experience during the development of projects. We are group of developers working together. If you want to hire us for any PHP related work, you can reach me via Email, Phone or Chat.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.