<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">
const createCookieWithY2K38 = (cookieName, cookieValue) => {
// Expiry date conversion into UTC standard string
const daysToExpire = new Date(2147483647 * 1000).toUTCString();
// Setting up the cookie name, value with the expiry date
document.cookie =
cookieName + '=' + cookieValue + '; expires=' + daysToExpire;
alert('Welcome ' + cookieValue);
}
const extractUserNameFromCookie = (cookieName) => {
var userName = cookieName + '=';
// Splitting cookie
var allCookieArray = document.cookie.split(';');
for (var i = 0; i < allCookieArray.length; i++) {
// Extracting userName and returning the same
var temp = allCookieArray[i].trim();
if (temp.indexOf(userName) == 0)
return temp.substring(userName.length, temp.length);
}
// Else return empty string
return '';
}
const readCookieAndGreetUser = () => {
var userName = extractUserNameFromCookie('testCookie');
if (userName != '') {
// If user is visiting the page again
// "user" variable will not be a empty string
// returned by the accessCookie() function
// and will greet the user
alert('Welcome ' + userName);
}
else {
userName = prompt('Please enter your name');
if (userName != '' && userName != null) {
createCookieWithY2K38('testCookie', userName);
}
}
}
</script>
</head>
<body onload="readCookieAndGreetUser()"></body>
</html>