Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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?

irt.org | Knowledge Base | JavaScript | Password | Q595 [ previous next ]

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?

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.