As long as you're setting and reading the cookie on your domain, you should be ok. You can't read or write cookies from other domains. Also, don't set a path for your cookie. By default, the path is "/", allowing all pages on your server to access the cookie. I used this with ASP, so it automatically wrote a session ID to the cookie, but if the only value in the cookie is your user's email address, change two lines in the JavaScript:
userInfo = document.cookie.split("\;");
userEmail = userInfo[0].split("\=");
to this
userInfo = document.cookie.split("\=");
userEmail = userInfo[1];
Whew! With that in mind, here's some code:
The first page:
Code:
<html>
<head>
<title>Page 1</title>
<script language="JavaScript">
<!--
function Set_Cookie(name,value,expires,path,domain,secure)
{ document.cookie = name + "=" +escape(value) + ( (expires) ? ";expires=" + expires: "") + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ( (secure) ? ";secure" : "");
}
// -->
</script>
</head>
<body>
<form name="myForm" action="" method="post" onSubmit="Set_Cookie('userEmail',document.myForm.emailAddress.value,'Mon, 20-Feb-2012 00:00:01 GMT')">
<input type="text" name="emailAddress">
<input type="submit" name="submit" value="Submit">
</form>
<a href="page2.html">Check cookie</a>
</body>
</html>
Page 2:
Code:
<html>
<head>
<title>Page 2</title>
<script language="JavaScript">
<!--
function populateEmail() {
userInfo = document.cookie.split("\;");
userEmail = userInfo[0].split("\=");
document.form2.email2.value = userEmail[1];
}
// -->
</script>
</head>
<body onLoad="populateEmail();">
<form name="form2" action="" method="post">
<input type="text" name="email2">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Bookmarks