<!DOCTYPE html>
<html lang="en-US">
<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>