JavaScript Cookie Attributes

To enhance the functionality of cookies, some optional attributes may be used in Javascript.

expires:
Used to maintain the state of a cookie up to the specified date and time.

document.cookie = 'name=Jai; expires=Sun, 19 May 2019 18:04:55 UTC'

max-age:
Used to maintain the state of a cookie up to the specified time in seconds.

document.cookie = 'name=Jai; max-age=3600'

path:
Used to expand the scope of the cookie to all the pages of a website.

document.cookie = 'name=Jai; path="/testFolder"'

domain:
Used to specify the domain for which the cookie is valid.

document.cookie = 'name=Jai; domain="826.a00.myftpupload.com";'

Example:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="set" onclick="setCookie()">
<input type="button" value="get" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username = COOKIE;expires=Sun, 28 Nov 2018 13:15:00 UTC";
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("No Cookie here");
}
}
</script>
</body>
</html>