Write and Read browser cookies using pure javascript

I will show you how to create and read cookies in browser in simple javascrpt , here is javascript code to write and read browser cookies :

        <script type=”text/javascript”>

function WriteCookie()
               if( document.myformSet.customer.value == “” ){
                  alert(“Enter some value!”);

   cookievalue= escape(document.myformSet.customer.value) + “;”;
               document.cookie=”name=” + cookievalue;
               alert(“Setting Cookies : ” + “name=” + cookievalue );

function ReadCookie()
               var allcookies = document.cookie;
               alert(“All Cookies : ” + allcookies );

               // Get all the cookies pairs in an array
               cookiearray  = allcookies.split(‘;’);

               // Now take key value pair out of this array
               for(var i=0; i<cookiearray.length; i++){
                  name = cookiearray[i].split(‘=’)[0];
                  value = cookiearray[i].split(‘=’)[1];
                  alert(“Key is : ” + name + ” and Value is : ” + value);

<form name=”myformSet” action=””>
            Enter name: <input type=”text” name=”customer”/>
            <input type=”button” value=”Set Cookie” onclick=”WriteCookie();”/>
<form name=”myformGet” action=””>
            <input type=”button” value=”Get Cookie” onclick=”ReadCookie()”/>


that’s code will create and show saved cookies on alert message.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s