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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Retain Script Question

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. Ive already done a tiny change that will make it so textareas will work. The trouble im 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.

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
 
Some suggestionns:

- Put semicolon delimiters in. I've never seen a script snippet with so many missing.

- Post the relevant HTML code (or the whole page, or a URL to it) - the error could be there.

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top