Storing Cookie Value (Javascript):   Return to List

// setCookie stores data in a cookie
function setCookie(cName,cValue,eDate,cPath,cDomain,cSecurity)
{
    var theCookie = cName + "=" + escape(cValue);    // Variable to hold the cookie string
    if (eDate) theCookie += "; expires=" + eDate.toGMTString();    // If an expire date was passed, then use it.
    if (cPath) theCookie += "; path=" + cPath;    // If a path was passed, then use it.
    if (cDomain) theCookie += "; domain=" + cDomain;    // If a domain was passed, then use it.
    if (cSecurity) theCookie += "; secure";    // If this is for a secure connection, then request one.
    document.cookie = theCookie;
}
// *** a more useful example **** setting first and last name ***
<HTML>
<HEAD>
<TITLE>This is a test</TITLE>
<SCRIPT LANGUAGE="JavaScript">
today = new Date();            
nextYear = new Date();        
today = today.getTime();    
nextYear.setTime(today + 365 * 24 * 60 * 60 * 1000);
expDate = nextYear.toGMTString();    

function setCookie(myForm)
{
    var fullName = myForm.firstname.value + " " + myForm.lastname.value
    var theCookie = "CarlLinksDbase=" + escape(fullName) + "; expires=" + expDate;
    document.cookie = theCookie;
}

</SCRIPT>
</BODY>
<FORM NAME="testform">
<INPUT TYPE="button" NAME="button" Value="Complete your first & last name below -- THEN click here" onClick="setCookie(testform)"><BR>
First name: <INPUT TYPE="text" NAME="firstname" Value="" defaultValue="" onClick = "null;"><BR>
Last name: <INPUT TYPE="text" NAME="lastname" Value="" defaultValue="" onClick = "null;"><BR>
</FORM>
</HTML>



Note to Webmaster