Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Memory Script

Status
Not open for further replies.

kyern

Programmer
Mar 30, 2006
36
US
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

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;
 
Here is where the form field values are set from the values retrieved from the cookie.
Code:
...
memorizearray[i].value = retrievedvalues[i];
...

memorizearray is an array of objects. The objects are the form fields. The form fields, called elements in the DOM, are assigned in this statement.
Code:
...
memorizearray[memorizearray.length]=theforms[i].elements[j];
...
In other words the expression

memorizearray.value

is another way of writing

theforms.elements[j].value

Except the subscripts do not correspond. The array memorizearray is one array including all fields in all forms. If you see what I mean.


HTH
 
Thank you! When the form fields are set into the array like that do they retain all their information? I need to check the type of filed like so memorizearray.type but it doesn't seem to work that way. Any suggestions?

Thanks again

David
 
Ignore the previous post, I have it completely working now. Thank you very much!

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top