Whenever we visit a website, cookies may be stored to retain information about our browsing activities. For instance, when we log in to a webpage using a username and password, a cookie is typically generated on our computer after successful authentication.
One notable example is Google's popular website. This search engine allows users to specify the number of search results they prefer to see on each page. Through the use of a cookie, this preference remains unchanged on individual computers, even after restarting multiple sessions. However, it is advisable to occasionally delete cookies because although some expire and are automatically deleted after a certain period, others may persist indefinitely.
How to Use Cookies in Javascript
Cookies can be manipulated with JavaScript, often employed in various tricks for bloggers. Unfortunately, cookies have gained a negative reputation due to potential misuse. Some websites may store and analyze users' browsing patterns without their knowledge. To counter this, most web browsers now include filtering systems that grant users the ability to decide the level of privacy they desire.
To observe a live demonstration of cookie functionality, please visit the mentioned page and refresh it multiple times. An alert window will appear, indicating the number of times you have visited the page. (Script source: javascriptkit.com)
While there are alternative methods to utilize cookies, one common approach involves employing JavaScript on the server-side. Below, we provide a simple script that can be used multiple times for various purposes. This script creates three functions: one for setting a cookie, another for reading it, and a final function for deleting it. In Blogger, you can incorporate this script by accessing the template HTML and adding it just before the </head> tag.
<script type='text/javascript'>//<![CDATA[// Set cookiefunction setCookie(name, value, expires, path, domain, secure) {document.cookie = name + "=" + escape(value) +((expires == null) ? "" : "; expires=" + expires.toGMTString()) +((path == null) ? "" : "; path=" + path) +((domain == null) ? "" : "; domain=" + domain) +((secure == null) ? "" : "; secure");}// Read cookiefunction getCookie(name){var cname = name + "=";var dc = document.cookie;if (dc.length > 0) {begin = dc.indexOf(cname);if (begin != -1) {begin += cname.length;end = dc.indexOf(";", begin);if (end == -1) end = dc.length;return unescape(dc.substring(begin, end));}}return null;}//delete cookiefunction eraseCookie (name,path,domain) {if (getCookie(name)) {document.cookie = name + "=" +((path == null) ? "" : "; path=" + path) +((domain == null) ? "" : "; domain=" + domain) +"; expires=Thu, 01-Jan-70 00:00:01 GMT";}}//]]></script>
var expiration = new Date();expiration.setTime(expiration.getTime() + 10000); //Expire after 10 secondssetCookie("cookiename","hello",expiration);}
var checkCookie = getCookie("cookiename");
<script type='text/javascript'>var expiration = new Date();expiration.setTime(expiration.getTime() + 10000);setCookie("cookiename","hello",expiration);var checkCookie = getCookie("cookiename");document.write(checkCookie);</script>
Comments
Post a Comment
All types of Comments are welcome