Thanks to everyone who helped me here last week; I’ve studied a lot and the DOM syntax is no longer confusing. Now I have a problem with cookies ?
I’m trying to save the state of a form into a cookie so that the form is restored per user preferences when they return to the page.
The page is generated by a perl script, and this script uses CGI variables (i.e, “myscript.cgi?foo=2&boo=2”) to control some display options not related to the form date I am trying to save in a cookie.
Saving to a cookie works fine — as long as it is saved from the default URL (no CGI variables tacked on). i.e., from page “
However, if user changes some other option (from another, different form in the same page), the URL may become “
Is there some easy way to ignore cgi variables in the url when saving a cookie?
Just in case it’s a script bug, here’s the relevant functions:
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” : “”);
}
function setupForm() {
if (userProfile) {getValues(userProfile);}
}
function getValues(string) {
getValue(string,”p1″, document.myprojects.p1, “select”);
getValue(string,”p2″, document.myprojects.p2, “select”);
getValue(string,”p3″, document.myprojects.p3, “select”);
getValue(string,”p4″, document.myprojects.p4, “select”);
getValue(string,”p5″, document.myprojects.p5, “select”);
getValue(string,”p6″, document.myprojects.p6, “select”);
getValue(string,”p7″, document.myprojects.p7, “select”);
getValue(string,”p8″, document.myprojects.p8, “select”);
for (var i=0;i<7+1;i++) {
getValue(string,”i”+i, eval(“document.myprojects.i”+i),”checkbox”);}
}
function replace(string,text,by) {
// Replaces text with by in string
var i = string.indexOf(text);
var newstr = ”;
if ((!i) || (i == -1)) {return string;}
newstr += string.substring(0,i) + by;
if (i+text.length < string.length) {
newstr += replace(string.substring(i+text.length,string.length),text,by);}
return newstr;
}
function onCheck(string) { if (string == “on”) {return true;} return false; }
function getValue(string,elementName,object,elementType) {
// gets value of elementName from string and populates object of elementType
var startPos = string.indexOf(elementName + “=”);
if (startPos > -1) {
startPos = startPos + elementName.length + 1;
var endPos = string.indexOf(“&”,startPos);
if (endPos == -1) { endPos = string.length; }
var elementValue = unescape(string.substring(startPos,endPos));
if (elementType == “text”) {object.value = elementValue;}
if (elementType == “password”) {object.value = elementValue;}
if (elementType == “select”) {object.selectedIndex = elementValue;}
if (elementType == “checkbox”) {object.checked = onCheck(elementValue);}
if (elementType == “radio”) {object[elementValue].checked = true;}
}
}
<SCRIPT TYPE=”text/javascript” LANGUAGE=”JavaScript”><!–
var today = new Date(); var expires = new Date(today.getTime() + (356 * 86400000));
var searchString = replace(self.location.search.substring(1),”+”,” “);
if (searchString.length > 0) Set_Cookie(“userProfile”,searchString,expires);
//if (searchString.length > 0) Set_Cookie(“userProfile”,searchString,expires,’/’);
var userProfile = Get_Cookie(“userProfile”);
if (!userProfile) {
document.write(‘<P><B>No project preferences found for you.</b> Select from below and save.’);
document.write(‘If you have trouble, please allow cookies from foo.com.’);
} else {
document.write(‘<P><B>Found the following project preferences.</b> Make changes and ‘);
document.write(‘save if desired.’);
}
thanks in advance for any help!