Setting Cookie through Javascript

Setting Cookie through Javascript

This entry is useful if you are trying to show popup message or image on any page via using cookie and want to display on once when page loaded. What it does it write cookie on user machine and check that cookie exist on next time and if it is then just doesn’t render that code. This may be useful for showing cookie usage message as well. what script is consist of two method which write cookie and read cookie on client side.

To write cookie

setCookie ('__your_cookie_id',1,365)

where first argument is your cookie id to set and value is second argument which is 1 in my case and third argument is 365 days for expiration.

To ready this cookie via web is simple:


and this will return cookie value.

if you would like to just check cookie exist on not then you can simple do by if statement like


code block if not exist


Here is two method is used to demonstrate this example :

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

function getCookie(c_name){
   var i,x,y,ARRcookies=document.cookie.split(";");
   for (i=0;i<ARRcookies.length;i++)
  if (x==c_name)
    return unescape(y);

Leave a Reply

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