Friday, July 9, 2010

Cookies

<html>
<head>
<title>:==:CIT COOKIE:==:</title>
<script language="javascript">
    function setCookie(name,value,expiry)
    {
        document.cookie=name+"="+escape(value)+((expiry==null)?"":(";expires="+expiry.toGMTString()))
        //escape=Encodes String objects so they can be read on all computers.
        //GMT-Green Wich Mean Time which is universal time in the format Tue,02 Apr 1996 02:04:57 GMT
    }
    function getCookie(name)
    {
        var s=name+"="
        //check if any cookie exits
        if(document.cookie.length>0)
        {
            //check if "name=" string exits
            if((i=document.cookie.indexOf(s))!=-1)
                {
                    //if specific cookie name with = sign exists
                    //get its length
                    i+=s.length
                    //check the position of semi colon in the cookie name
                    if((j=document.cookie.indexOf(";",i))==-1)
                    {
                        //when semi colon found use substring function to
                        //get the value of cookie from the position of
                        //"=" sign trapped in 'i' variable to the last position
                        //of cookie value which is trapped in 'j' variable
                        j=document.cookie.length
                        return unescape(document.cookie.substring(i,j))
                    //unescape decodes string objects encoded with the escape method
                    }
                }
        }
       
    }
    function registration(name)
    {
        var date=new Date()
        var expiry=new Date()
        expiry.setTime(date.getTime()+(24*60*60*1000))
        setCookie("MyNepal",name,expiry)
    }
   
</script>
</head>

<body>
<script language="javascript">
var name=getCookie("MyNepal")
if(name!=null)
    {
        document.write("Welcome to CITE,"+name)
        document.write("<p>You have already registered with us</p>")
    }
    else
    {
        document.write("Welcome to CITE")
        document.write("<p>Please register yourself with CITE</p>")
        document.write('<form name="form">')
        document.write('<input type="text" size=20 name="nameValue">')
        document.write('<input type="button" value="Register" onClick="registration(document.form.nameValue.value);alert(&quot;Thank You.You have been registered with CITE&quot;)">')
        document.write('</form>')
    }
       
</script>

</body>
</html>