JavaScript Program to Create a Cookie and Setting expiration Time

Write a JavaScript program to create a cookie and setting expiration time.



Source Code
<html>
<head>
    <title>Set Cookie expiration time example</title>
    <script>
        function setCookie() 
        {
            var name = document.getElementById('person').value
            var expireDate = new Date()
            alert(expireDate)
            expireDate.setMonth(expireDate.getMonth() + 3)
            document.cookie = "name=" + name + "; expires="+expireDate.toGMTString()
        }
        function readCookie() 
        {
            var cookie = document.cookie
            var panel = document.getElementById('panel')

            if(cookie == "")
                panel.innerHTML = "Cookie not found"
            else
                panel.innerHTML = cookie
        }
    </script>
</head>

<body>
<form name="myForm">
    Enter your name <input type="text" id="person"/><br/>
    <input type="button" value="Set Cookie" onclick="setCookie()"/>
    <input type="button" value="Read Cookie" onclick="readCookie()"/>
</form>
<p id="panel"></p>
</body>

</html>
Output