codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  script help  cwilkey at 17:49 on Friday, April 01, 2005
 

How can I modify this code to only save specified fields. Currently, it saves every form field within the form....I want to be able to specify which ones get stored as cookies.


<SCRIPT LANGUAGE="JavaScript">
// Cookie Functions //////////////////// (:)
// Set the cookie.
// SetCookie('your_cookie_name', 'your_cookie_value', exp);
// Get the cookie.
// var someVariable = GetCookie('your_cookie_name');



var expDays = 1;

var exp = new Date();

exp.setTime(exp.getTime() + (expDays*24*60*60*1000));



function getCookieVal (offset) {

var endstr = document.cookie.indexOf (";", offset);

if (endstr == -1) { endstr = document.cookie.length; }

return unescape(document.cookie.substring(offset, endstr));

}



function GetCookie (name) {

var arg = name + "=";

var alen = arg.length;

var clen = document.cookie.length;

var i = 0;

while (i < clen) {

var j = i + alen;

if (document.cookie.substring(i, j) == arg) return getCookieVal (j);

i = document.cookie.indexOf(" ", i) + 1;

if (i == 0) break;

}

return null;

}



function SetCookie (name, value) {

var argv = SetCookie.arguments;

var argc = SetCookie.arguments.length;

var expires = (argc > 2) ? argv[2] : null;

var path = (argc > 3) ? argv[3] : null;

var domain = (argc > 4) ? argv[4] : null;

var secure = (argc > 5) ? argv[5] : false;

document.cookie = name + "=" + escape (value) +

((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +

((path == null) ? "" : ("; path=" + path)) +

((domain == null) ? "" : ("; domain=" + domain)) +

((secure == true) ? "; secure" : "");

}

// cookieForms saves form content of a page.
// use the following code to call it:
// <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">
// It works on text fields and dropdowns in IE 5+
// It only works on text fields in Netscape 4.5





function cookieForms() {

var mode = cookieForms.arguments[0];



for(f=1; f<cookieForms.arguments.length; f++) {

formName = cookieForms.arguments[f];



if(mode == 'open') {

cookieValue = GetCookie('saved_'+formName);

if(cookieValue != null) {

var cookieArray = cookieValue.split('#cf#');



if(cookieArray.length == document[formName].elements.length) {

for(i=0; i<document[formName].elements.length; i++) {



if(cookieArray.substring(0,6) == 'select') { document[formName].elements.options.selectedIndex = cookieArray.substring(7, cookieArray.length-1); }

else if((cookieArray == 'cbtrue') || (cookieArray == 'rbtrue')) { document[formName].elements.checked = true; }

else if((cookieArray == 'cbfalse') || (cookieArray == 'rbfalse')) { document[formName].elements.checked = false; }

else { document[formName].elements.value = (cookieArray) ? cookieArray : ''; }

}

}

}

}



if(mode == 'save') {

cookieValue = '';

for(i=0; i<document[formName].elements.length; i++) {

fieldType = document[formName].elements.type;



if(fieldType == 'password') { passValue = ''; }

else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements.checked; }

else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements.checked; }

else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements.options.selectedIndex; }

else { passValue = document[formName].elements.value; }



cookieValue = cookieValue + passValue + '#cf#';

}

cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter



SetCookie('saved_'+formName, cookieValue, exp);

}

}

}

// End -->

</script>









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: problem with integer numbers
•  fstream and char array
•  Re: Trying to access method A`s variables or resources from method B
•  Using an image to control/change a simple drop menu???
•  Hey Toaders check this out
•  Print and print preview file on the website without using the File - Print on the IE in ASP command
•  Re: Custom Form Validation Using JavaScript
•  Re: Post&submit text to a form in java
•  JDI popframes method


Recent Articles
Communicating with the Database (Using ADO)
MagicGrid
Simple Thumbnail Browsing Solution
Type Anywhere
A Better Moustrap: FmtDate to replace FormatDateTime
ASP.NET Forum Source Code
Internal Search Engine
Javascript Growing Window
Simple date validation
Search engine friendly URLs using ASP.NET (C#.NET)


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005