How to Use Cookies in Javascript
Each time we visit a website, a cookie may be stored to "remember" what we did while browsing the website. This can happen, for example, when we visit a website where we need to log in with a username and password. In this case, cookies are usually stored on our computer after login authentication.
An example of this is the famous Google site. This search engine allows users to choose how many search results they want to see on each page. Thanks to cookies, this configuration remains unchanged for each computer, even after several restarts. That said, it's good to delete them from time to time, because while some will expire and get deleted automatically after a while, some will never expire.
Like most things, cookies are subject to abuse, which is why they have a bad reputation. For example, some websites may store and learn about users' browsing habits without their knowledge. That's why most browsers already include a filtering system and can decide whether privacy is guaranteed or not.
If you want to see the first example of how cookies work, visit and refresh this page several times. You should get a warning window telling you how many times you have visited the page.
Script source: javascriptkit.com
There may be other ways to use cookies, but the server may do so through JavaScript. Below is a simple script that can even be used multiple times for different purposes. Basically, what the following script does is create three jobs: the first is to set the cookie, the second is to read it, and the last is to delete it. We can implement it by accessing the HTML form in Blogger and adding it to the frontend Note:
Once we do that, we just need to enclose the name and value in quotes when calling the function to set the cookie. Additionally, we will set the expiration date by taking the current time (in milliseconds) and adding the required number of minutes (in milliseconds);
The scan function is used in the same way as the read-only function by clicking on the cookie name. The setCookie values for "domain" and "security" are not used. If you're using it on a subdomain, use "domain" in your javascript cookie because the cookie is set on the device's subdomain, but it needs to be accessible on your site.com domain.
The combination of these three functions will allow us to process cookies for specific purposes, which we will see in the near future.
An example of this is the famous Google site. This search engine allows users to choose how many search results they want to see on each page. Thanks to cookies, this configuration remains unchanged for each computer, even after several restarts. That said, it's good to delete them from time to time, because while some will expire and get deleted automatically after a while, some will never expire.
Like most things, cookies are subject to abuse, which is why they have a bad reputation. For example, some websites may store and learn about users' browsing habits without their knowledge. That's why most browsers already include a filtering system and can decide whether privacy is guaranteed or not.
If you want to see the first example of how cookies work, visit and refresh this page several times. You should get a warning window telling you how many times you have visited the page.
Script source: javascriptkit.com
There may be other ways to use cookies, but the server may do so through JavaScript. Below is a simple script that can even be used multiple times for different purposes. Basically, what the following script does is create three jobs: the first is to set the cookie, the second is to read it, and the last is to delete it. We can implement it by accessing the HTML form in Blogger and adding it to the frontend Note:
Once we do that, we just need to enclose the name and value in quotes when calling the function to set the cookie. Additionally, we will set the expiration date by taking the current time (in milliseconds) and adding the required number of minutes (in milliseconds);
var ends = new Date();The code above sets the cookie named cookiename with the value hello and sets an expiration time of 10 seconds after it is set ( 10000ms = 10 seconds). If we want to return the value of this cookie, we need to use a second function called cookie:
expired.setTime(expired.getTime() + 10000 ); // end after 10 seconds
setCookie ("cookiename", " hello ", expired);
}
var checkCookie = getCookie(" cookiename ");By adding this code below Tagged, we create a cookie with the value " hello ", which you can display on the screen if you want. Cookies will disappear after 10 seconds.
The scan function is used in the same way as the read-only function by clicking on the cookie name. The setCookie values for "domain" and "security" are not used. If you're using it on a subdomain, use "domain" in your javascript cookie because the cookie is set on the device's subdomain, but it needs to be accessible on your site.com domain.
The combination of these three functions will allow us to process cookies for specific purposes, which we will see in the near future.
Post a Comment for "How to Use Cookies in Javascript"