Showing posts with label Cookies. Show all posts
Showing posts with label Cookies. Show all posts

Monday, March 13, 2017

What is the difference between Local Storage and Cookies

Hi Friends,

In this post we are going to talk about when to use Local storage and Cookies.

To begin with, this is an extremely broad scope question, and a lot of pros/cons will be contextual to the situation.

Please find the below differences.


Local Storage
Cookies
Good to store large amount of data, up to 4MB
Good for small amount of data.
Cookies give you a limit of 4096 bytes (4095, actually) per cookie
Local Storage allow you to store JavaScript primitives but not Objects or Arrays (it is possible to JSON serialize them to store them using the APIs)
Cookies only allow you to store strings
Local storage data is not sent to the server on every request (HTTP header) as it is purely at the client side
All data is transferred to and from server, so bandwidth is consumed on every request
It stores data with no expiration date, and gets cleared only through JavaScript, or clearing the Browser Cache / Locally Stored Data
Can specify timeout period so that cookies data are removed from browser
Local storage stores the data in the form of key and value

Not supported by anything before: IE 8, Firefox 3.5, Safari 4, Chrome 4, Opera 10.5, iOS 2.0, Android 2.0
local storage can only be read client-side



Hope this helps...

Tuesday, September 6, 2011

Get Cookie and set cookie with Javascript


Below exercise is to get and set the cookie using client side and asp.net ::

Using Java script:

<script type="text/javascript">
    function setCookie(name,value,days) {
       if (days) {
          var date = new Date();
          date.setTime(date.getTime()+(days*24*60*60*1000));
          var expires = "; expires="+date.toGMTString();
       }
       else var expires = "";
       document.cookie = name+"="+value+expires+"; path=/;secure";
    }

    function getCookie(name) {
       var nameEQ = name + "=";
       var ca = document.cookie.split(';');
       for(var i=0;i < ca.length;i++) {
          var c = ca[i];
          while (c.charAt(0)==' ') c = c.substring(1,c.length);
          if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
       }
       return null;
    }
   
</script>


Using asp.net Server side:

HttpCookie cookie;



if (HttpContext.Current.Request.Cookies["CookieName"] != null)

{

    cookie = HttpContext.Current.Request.Cookies["CookieName"];

}

else

{

    cookie = new HttpCookie("CookieName");

}



cookie["CookieKey"] = "YourValue";

cookie.Expires = DateTime.Now.AddYears(10);

HttpContext.Current.Response.Cookies.Add(cookie);
 
 
Hope this helps... 

Popular Posts