- Back to Home »
- ASP.Net , HTML , jQuery »
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:
- $(function() {
- $.cookie("myVar", "Hello World!");
- $.cookie("myVar", { path: '/' }); // Sets the cookie at the root level
- });
TO READ:
- $(function() {
- alert($.cookie("myVar"));
- });
TO DELETE:
- $(function() {
- $.removeCookie("myVar");
- $.removeCookie('myVar', { path: '/' }); // Removes the cookie from the root level
- });
CODE VIEW:
- <!DOCTYPE html>
- <html>
- <head>
- <title>jQuery Session</title>
- <script type='text/javascript' src='jquery-1.9.1.js'></script>
- <script type="text/javascript" src="jquery.cookie.js"></script>
- </head>
- <body>
- <script type='text/javascript'>
- $(window).load(function() {
- // To Store
- $(function() {
- $.cookie("myVar", "Hello World!");
- });
- // To Read
- $(function() {
- alert($.cookie("myVar"));
- });
- });
- </script>
- </body>
- </html>