JavaScript Cookies update
JavaScript Cookies update
To update a cookie, you just have to rewrite a cookie with the same key name to be updated. This will overwrite the old value.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var name = "Steve";
var uName = "";
//Cookie write
document.cookie = "username="+name+";path=/";
//Cookie read all
allcookie=(document.cookie);
//Convert string to array
cookiearray = allcookie.split(";");
//Run through array
cookiearray.forEach(function(element) {
if(element.indexOf("username")>-1)
uName=element.slice((element.indexOf("username=")+9), element.length);
});
//Print the name
document.write("Before Update <br>Name = " +uName);
//Cookie update
document.cookie = "username= John;path=/";
//Cookie read all
allcookie=(document.cookie);
//Convert string to array
cookiearray = allcookie.split(";");
//Run through array
cookiearray.forEach(function(element) {
if(element.indexOf("username")>-1)
uName = element.slice((element.indexOf("username=")+9), element.length);
});
// Print the name
document.write("<br><br>After Update <br>Name = " +uName);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.