Forum Moderators: open

Message Too Old, No Replies

cookies losing data

         

natty

3:00 pm on Apr 23, 2003 (gmt 0)

10+ Year Member



hi all,

ive prolly already posted about this, but its still driving me mad, so here i go again.

have dynamic dropdowns in step 1.
goto step 2, want to back button to step 1.
because the dropdowns were built dynamically , the browser wont hold the info, so i safve it to a cookie (only for IE6, and NN7 - which dont like to hold it in a hidden field... - but thats another story)
so i grab all the data of the dropdowns and put it in a delimted string (delimiter =!). on backing to step 1, hits an onload function which grabs the cookie and fills the dropdowns.. wha hey, easy huh..
well no sadly, because half the time (ok more like 15%) it fails to refil the dropdowns. I have alerted cookie values, cookie names, and all that stuff, to attempt to see whre the data is going walkies.
but cant sort it out..

here is a snippet of code.


var agent = navigator.userAgent;
version = agent.split('/');
if (version.length >= 3){
if(version[3].charAt(0) == 7){
usecookie = true;//NN7
}
}else if(version[1].indexOf('MSIE 6.0')!= -1){
usecookie = true;//IE 6
}else{
usecookie = false;//everything else
}
if(usecookie == true){
cookieVal = getCookie(cookieID);
}
valsArray = cookieVal.split('!');
for (i = 0;i < valsArray.length-1;i++){
//loop round each saved fields value
var fieldVals = new Array();
var itemArray = new Array();
fieldObj = 'document.forms[0].' + fieldsToSave[i]
fieldToFill = eval(fieldObj);
fieldVals = valsArray[i].split('+');
//for the return/single field
for (x=0;x<fieldVals.length;x++){
//loop round all the field options
if (x == fieldVals.length-1){defItem = fieldVals[x];break;}
itemArray[x] = [fieldVals[x].split(',')[0], fieldVals[x].split(',')[1]];
}
fillselect(fieldToFill, itemArray,0,defItem);
}

and ...


function getCookie(name){
var cname = name + '=';
var dc = document.cookie;
if (dc.length > 0){
begin = dc.indexOf(cname);
if (begin!= -1){
begin += cname.length;
end = dc.indexOf(';', begin);
if (end == -1){
end = dc.length;
}
return unescape(dc.substring(begin, end));
}
}
return null;
}

DrDoc

7:55 pm on Apr 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In short, there's no 100% fool proof solution. The page will break in some browsers, sometimes seemingly random. There are a lot of odds and ends to take into consideration: cookie capabilities, browser, cache, proxy, user settings... in the end, the time between page load and back-button-click also matters.

To me it seems like you want to be able to user the back button to go to the previous page. Well, some browsers will reload the page, others will not. Some browsers will display the page in the state is was loaded the first time, others in the state it was when you left it.

It's almost impossible to find a solution that adresses all these issues (and all unknown issues as well).

Have you considered explicitly putting a "Back" button tied to a form? This form could then take you forward to the previous page, passing parameters that will reset the values to what they should be. That's the best solution I can think of.

Don't try to change the way a browser handles its builtin back button functionality. It is doomed to fail and cause new problems.

Rethinking the problem from a different perspective seems to be your best bet. And it will save you some hard work and a lot of headaches...