Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q595 Can I use cookies to allow a user to check a box next to their username to save it to their browser and check another box to save the password?

You are here: irt.org | FAQ | JavaScript | Password | Q595 [ previous next ]

Try:

<html>
<head>
<script language="JavaScript"><!--
function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

var today = new Date();
var expires = new Date(today.getTime() + (56 * 86400000));

function set() {
    if (document.logonForm.elements[1].checked)
        Set_Cookie("userid",document.logonForm.userid.value,expires);

    if (document.logonForm.elements[3].checked)
        Set_Cookie("password",document.logonForm.password.value,expires);
}

function get() {
    userid = Get_Cookie("userid")
    if (userid != null) {
        document.logonForm.userid.value = userid;
        document.logonForm.elements[1].checked = true;
    }
    password = Get_Cookie("password")
    if (password != null) {
        document.logonForm.password.value = password;
        document.logonForm.elements[3].checked = true;
    }
}

//--></script>
</head>

<body onLoad="get()">

<form name="logonForm" onSubmit="return set();">
<p>Userid: <input type="input" name="userid"> Save <input type="checkbox">
<p>Password: <input type="password" name="password"> Save <input type="checkbox">
<p><input type="reset"> <input type="submit">
</form>

</body>
</html>

Feedback on 'Q595 Can I use cookies to allow a user to check a box next to their username to save it to their browser and check another box to save the password?'

©2018 Martin Webb