Tuesday, January 15, 2013

Javascript example - Set cookies and Remove cookies


A cookie is a variable that is stored on the users browser and depends on browser's configuration. A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is usually a small piece of data sent from a server and stored in a user's web browser while a user is browsing a website. When the user browses the same website in the future, the data stored in the cookie can be retrieved by the website to notify the user's previous activity like clicking particular buttons, logging in, or a record of which pages were visited.

Example - The first time a user visits to your web page, he or she must fill in her/his name. The name is then stored in a cookie. Next time when the user visits at your page, he or she could get a welcome message like "Welcome Alex!" The name is retrieved from the stored cookie. In the below example we will see how to set cookie and remove cookie.

Below is the tested code, you can directly save it and see the output.



Note - Chrome does not support to set cookies from local machine, it will work through server.