I am trying to customize this script so it can retain values of both selects and checkbox fields. I've already done a tiny change that will make it so textareas will work. The trouble I'm having is probably very basic. I can't figure out where the value is being assigned to the form field. I expected to see a loop with a .value being assigned from the scripts array however that isn't the case. Any insight would be wonderful.
I've went ahead and formatted this script correctly. This was a third party script so I had avoided making changes that I didn't have to. If this makes it easier for the previous commenter to read that is excellent. There is no external html, this is a modern javascript it is included in the header and just works.
Thanks
David
I've went ahead and formatted this script correctly. This was a third party script so I had avoided making changes that I didn't have to. If this makes it easier for the previous commenter to read that is excellent. There is no external html, this is a modern javascript it is included in the header and just works.
Thanks
David
Code:
//Time in days to save form fields values after last visit
//Set to different value to reset cookie (ie: "101 days" instead of "100 days"):
var memoryduration="100 days";
function setformobjects() {
var theforms=document.forms;
memorizearray=new Array();
for (i=0; i< theforms.length; i++) {
for (j=0; j< theforms[i].elements.length; j++){
if (theforms[i].elements[j].className.indexOf("memorize")!=-1) {
memorizearray[memorizearray.length]=theforms[i].elements[j];
}
}
}
var retrievedvalues=get_cookie("mvalue"+window.location.pathname);
if (retrievedvalues!="") {
retrievedvalues=retrievedvalues.split("|");
if (retrievedvalues[retrievedvalues.length-1]!=parseInt(memoryduration)) //reset cookie if var memoryduration has changed
resetcookie("mvalue"+window.location.pathname);
else{
for (i=0; i<memorizearray.length; i++) {
if (retrievedvalues[i]!="empty_value") {
memorizearray[i].value=retrievedvalues[i];
}
}
}
}
}
function get_cookie(Name) {
var search = Name + "=";
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search);
if (offset != -1) { // if cookie exists
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) {
end = document.cookie.length;
}
returnvalue=unescape(document.cookie.substring(offset, end));
}
}
return returnvalue;
}
function resetcookie(id){
var expireDate = new Date();
expireDate.setDate(expireDate.getDate()-10);
document.cookie = id+"=;path=/;expires=" + expireDate.toGMTString();
}
function saveformvalues(){
var formvalues=new Array(), temp;
for (i=0; i<memorizearray.length; i++) {
temp=memorizearray[i].value!=""? memorizearray[i].value : "empty_value";
formvalues[formvalues.length]=escape(temp);
}
formvalues[formvalues.length]=parseInt(memoryduration);
formvalues=formvalues.join("|");
var expireDate = new Date();
expireDate.setDate(expireDate.getDate()+parseInt(memoryduration));
document.cookie = "mvalue"+window.location.pathname+"="+formvalues+"; path=/;expires=" + expireDate.toGMTString();
}
if (window.addEventListener)
window.addEventListener("load", setformobjects, false);
else if (window.attachEvent)
window.attachEvent("onload", setformobjects);
else if (document.getElementById)
window.onload=setformobjects;
if (document.getElementById)
window.onunload=saveformvalues;