Posted by : Jebastin Wednesday, 29 January 2014

Usage of Cookies in ASP.NET using C# or jQuery. A cookie is a piece of data which is sent from a website and stored locally by the user’s browser. Cookies are needed because HTTP is stateless. This means that HTTP itself has no way to keep track of a user’s previous activities. One way to create state is by using cookies.

jQuery:

TO STORE:

  1. $(function() {
  2. $.cookie("myVar", "Hello World!");
  3. $.cookie("myVar", { path: '/' });   // Sets the cookie at the root level
  4. });

TO READ:

  1. $(function() {
  2.  alert($.cookie("myVar"));
  3. });

TO DELETE:

  1. $(function() {
  2.  $.removeCookie("myVar");
  3. $.removeCookie('myVar', { path: '/' });    // Removes the cookie from the root level
  4. });

CODE VIEW:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>jQuery Session</title>
  5. <script type='text/javascript' src='jquery-1.9.1.js'></script>
  6. <script type="text/javascript" src="jquery.cookie.js"></script>
  7. </head>
  8. <body>
  9. <script type='text/javascript'>
  10. $(window).load(function() {
  11.  // To Store
  12. $(function() {
  13. $.cookie("myVar", "Hello World!");
  14. });
  15. // To Read
  16. $(function() {
  17.  alert($.cookie("myVar"));
  18. });
  19. });
  20. </script>
  21. </body>
  22. </html>

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -