I have been trying to create & use a cookie to remember the user's ID. All the different recomended coding I've found doesn't work - or I don't understand cookies!
By playing around with the code, it seems I managed to set up a cookie. But, I can't delete it or set up a new one. Furthermore the cookie that was seemingly set up doesn't givee me the necessary info.
Everytime I run the code the cookie displayed is always the same: "RemID; UserID=yes". No matter what I do I always get this.
My latest code follows below.
Can you please help me?
Thank you.
<html>
<head>
<title>Log In</title>
<!-- ============================================= -->
</head>
<body onload="mystart()">
<form name="ET_Login" action="ET_HomePage.html" method="POST" onsubmit="mysubmit()">
<!-- ===================== JavaScript ======================== -->
<script type="text/javascript">
//=============================================
function mystart()
{document.getElementById('UserID').focus();
getCookie("RemUserID");
alert(document.cookie) // <--for degugging only.
if (document.cookie.length == 0)
{}
else {document.getElementById("UserID").value = document.cookie.substring(0,(document.cookie.length));
document.getElementById('pswd').focus();}}
//=============================================
function mysubmit()
{if (document.getElementById("RemBox").checked)
{createCookie("RemUserID", document.getElementById("UserID").value, "1");}
else {alert("go erase")
eraseCookie("UserID");}}
//=============================================
function createCookie(name, value, days)
{if (days)
{var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();}
else {var expires = "";
document.cookie = name+"="+value+expires+"; path=/";}}
//=============================================
function getCookie(name)
{var ca = document.cookie.split(';');
var nameEQ = name + "=";
for(var i=0; i < ca.length; i++)
{var c = ca;
while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
if (c.indexOf(nameEQ) == 0)
{return c.substring(nameEQ.length, c.length);}}
return null;}
//=============================================
function deleteCookie(name)
{createCookie(name, "", -1);}
//=============================================
</script>
<noscript>Your browser does not support JavaScript!</noscript>
<!-- =================================================================== -->
<h1 align=center><i><b>
User ID:
<input name="UserID" size="15" maxlength="15">
<br>
Password:
<input type="password" name="pswd" size="15" maxlength="15">
<br>
<input type="checkbox" name="RemBox" checked><font size="2">Remember my User ID.</font>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
By playing around with the code, it seems I managed to set up a cookie. But, I can't delete it or set up a new one. Furthermore the cookie that was seemingly set up doesn't givee me the necessary info.
Everytime I run the code the cookie displayed is always the same: "RemID; UserID=yes". No matter what I do I always get this.
My latest code follows below.
Can you please help me?
Thank you.
<html>
<head>
<title>Log In</title>
<!-- ============================================= -->
</head>
<body onload="mystart()">
<form name="ET_Login" action="ET_HomePage.html" method="POST" onsubmit="mysubmit()">
<!-- ===================== JavaScript ======================== -->
<script type="text/javascript">
//=============================================
function mystart()
{document.getElementById('UserID').focus();
getCookie("RemUserID");
alert(document.cookie) // <--for degugging only.
if (document.cookie.length == 0)
{}
else {document.getElementById("UserID").value = document.cookie.substring(0,(document.cookie.length));
document.getElementById('pswd').focus();}}
//=============================================
function mysubmit()
{if (document.getElementById("RemBox").checked)
{createCookie("RemUserID", document.getElementById("UserID").value, "1");}
else {alert("go erase")
eraseCookie("UserID");}}
//=============================================
function createCookie(name, value, days)
{if (days)
{var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();}
else {var expires = "";
document.cookie = name+"="+value+expires+"; path=/";}}
//=============================================
function getCookie(name)
{var ca = document.cookie.split(';');
var nameEQ = name + "=";
for(var i=0; i < ca.length; i++)
{var c = ca;
while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
if (c.indexOf(nameEQ) == 0)
{return c.substring(nameEQ.length, c.length);}}
return null;}
//=============================================
function deleteCookie(name)
{createCookie(name, "", -1);}
//=============================================
</script>
<noscript>Your browser does not support JavaScript!</noscript>
<!-- =================================================================== -->
<h1 align=center><i><b>
User ID:
<input name="UserID" size="15" maxlength="15">
<br>
Password:
<input type="password" name="pswd" size="15" maxlength="15">
<br>
<input type="checkbox" name="RemBox" checked><font size="2">Remember my User ID.</font>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>