|
Setting a Cookie in JavaScript
This page demonstrates how to use JavaScript to set a simple
Here's the JavaScript that performs all of this:
<SCRIPT LANGUAGE = "JavaScript">
<!--
// This function gets things set up, creates the cookie,
// and then moves to the next page
//
function BakeIt() {
// Get the value of the "data" field in the form
var cookieData = document.cookieForm.data.value;
// See if the data was entered
if (cookieData != null) {
// Use this variable to set the name of the cookie
var cookieName = "Name";
// Use this variable to set the number of days after which the cookie will expire
var days = 1;
// Calculate the expiration date
var expires = new Date ();
expires.setTime(expires.getTime() + days * (24 * 60 * 60 * 1000));
// Set the cookie
SetCookie(cookieName, cookieData, expires);
}
// Move to the next Cookies page
// Note that this is optional; you can delete this line if you want
window.location = "cookies2.asp";
}
function SetCookie(cookieName, cookieData, expireDate) {
document.cookie = cookieName + "=" + escape(cookieData) + "; expires=" + expireDate.toGMTString();
}
//-->
</SCRIPT>
To use this script, you need to do the following:
|