- Create a <a> tag or a Button on click of that you can set the cookie value.
- Just paste the script code with in the header tag.
- getCookieValue is the function through which you can get the value of the cookie.
- This is used to create a cookie which is created on the click event of anchor tag,which will be valid fpr 7 days in ypur browser.
<div>
<a href=”#” onclick=”set_cookie();”>Create Cookie</a>
</div>
<script type=”text/javascript”>
function set_cookie() {
var cookie_name = “cname”;
var cookie_value =”hgf”;
var lifespan_in_days = 7;
document.cookie = cookie_name + “=” + encodeURIComponent(cookie_value);
var cookie_string = document.cookie.indexOf(“cname”);
var cval = getCookieValue(“cname”);
}
function getCookieValue(key) {
currentcookie = document.cookie;
if (currentcookie.length > 0) {
firstidx = currentcookie.indexOf(key + “=”);
if (firstidx != -1) {
firstidx = firstidx + key.length + 1;
lastidx = currentcookie.indexOf(“;”, firstidx);
if (lastidx == -1) {
lastidx = currentcookie.length;
}
return unescape(currentcookie.substring(firstidx, lastidx));
}
}
return “”;
}
</script>
hello sir ,
your article very super .but demo with coding more useful.