Closed Thread Icon

Preserved Topic: DHTML, JavaScript and hiding DIV's Pages that link to <a href="https://ozoneasylum.com/backlink?for=18277" title="Pages that link to Preserved Topic: DHTML, JavaScript and hiding DIV&amp;#039;s" rel="nofollow" >Preserved Topic: DHTML, JavaScript and hiding DIV&#039;s\

 
Author Thread
Carnage
Nervous Wreck (II) Inmate

From:
Insane since: May 2001

posted posted 08-27-2001 20:08

I'm using div's with onclick's to hide and unhide certain tables by clicking on an image of a unchecked & checked box that I made. It works great, everything does as it should...............

EXCEPT!

When I submit the form and something is incorrect and they have to go back to the page, it resets to all the defaults (which is all of them being closed and unchecked).

Question is: How do I get the page to remember what state it was in?

Thanks,

Carnage

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-27-2001 20:57

Cookies?

Carnage
Nervous Wreck (II) Inmate

From:
Insane since: May 2001

posted posted 08-27-2001 21:21

Thought about cookies, but I don't want to do that unless it's the only option.

Any other ideas please?

Carnage

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 08-27-2001 21:31

Pugzly is right - cookies or if you want to go server side you could probably use sessions to record the final state of the page when the form is submitted (I'm not sure how tricky that would be but somewhere here might know). I'm not sure what your reluctance is - you could have a small message saying that browsers with cookies enabled will have a better experience on your page.

Emps

Carnage
Nervous Wreck (II) Inmate

From:
Insane since: May 2001

posted posted 08-27-2001 23:14

Well the reluctance is, now I gotta learn how to do cookies properly with DIV tags and onclick events.

Oh well, guess I'll go search for some cookie tutorial sites to brush up on em.

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 08-27-2001 23:44

Yes, cookies. But sometimes pages just instruct you to hit the "back" button and that should retain the last page's state. I guess your page has an init function that resets everything? The only other way would be to save the information into a database and make sure you pass the user's id to each page. That is probably a lot more trouble than using the cookies.

On my home page I have several different pages of divs that people can view. I save the last viewed page id out to a cookie onunload of the page and if a cookie exists for that user I just load it up when they come back. I'm no expert on cookies but these functions work ok for me:


// Cookie crumbs
var days2expire=1; // cookie expiration
function saveCookies() {
setCookie('_purplescape_state', pageState, days2expire);
}

function setCookie( name, value, expdays ) {
var path1="; path=/";
var expires = new Date();
if( expdays ) {
expires.setTime(expires.getTime() + (expdays * 24 * 60 * 60 * 1000));
document.cookie = name + "=" + escape( value ) + "; expires=" + expires.toGMTString() + path1;
} else {
document.cookie = name + "=" + escape( value ) + path1;
}
}

function getCookie( name ) {
var dcookie = document.cookie;
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
while( cbegin<clen ) {
var vbegin = cbegin + cname.length;
if( dcookie.substring( cbegin, vbegin ) == cname ) {
var vend = dcookie.indexOf( ";", vbegin );
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0) break;
}
return '';
}

function loadCookies() {
if( getCookie( '_purplescape_state' ) ) {
pageState = getCookie( '_purplescape_state' );
}
}
// end Cookie crumbs


My body tag just does this:

<body onLoad="loadCookies();" onUnload="saveCookies();">

You can add as many different cookies as you like to this scheme. I'm just using one in this example. I hope this helps.

Carnage
Nervous Wreck (II) Inmate

From:
Insane since: May 2001

posted posted 08-27-2001 23:54

So then, if I have 2 divs showing and the other like 4 divs hidden, this would save them as it is.

Now, if the same person comes back and wants to send another submittal (it's a form btw so it's taking in information) is there any way to tell that this is a new page and then to reset the cookie or what?

So like if the submit button goes, and then the email is sent and whatnot and everything is good, can I clear the cookies somehow, so when they go back to the page it shows up default again?

Thanks,

Carnage

Carnage
Nervous Wreck (II) Inmate

From:
Insane since: May 2001

posted posted 08-28-2001 00:23

Well I tried your code with changing the purple to a different name in each place. And it doesn't save anything. When I go back to the page all my divs are setup the way they are default. No state is saved.

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 08-28-2001 01:01

Carnage,

No, it's a bit more involved than that. What the code I posted does is allow you to save a value(s) into cookies. So in my example the saveCookies() saves only one value "pageState" into a cookie named "_purplescape_state".

You could set up your page to call the saveCookies() function and save *every* value of *every* field in your form. Then when you reload you need to read get the cookies saved for *each* value you saved and repopulate your form values.

Does this make sense? I know it is probably a lot more work than you thought you had to do but it is possible.

Carnage
Nervous Wreck (II) Inmate

From:
Insane since: May 2001

posted posted 08-28-2001 01:14

Ok here is some of my code.

<div id="crim_check_off"><div onclick="turnon('crim_table'); turnoff('crim_check_off'); turnon('crim_check_on');"><img src="images/check_off.gif" border="0" style="cursor:hand;"></div></div>
<div id="crim_check_on"><div onclick="turnoff('crim_table'); turnoff('crim_check_on'); turnon('crim_check_off');"><img src="images/check_on.gif" border="0" style="cursor:hand;"></div></div>


<div id="crim_table">Help Me</div>

Now what I have is like 6 sections like this. And I need to store wether the div id of crim_check_off is either on or off and same for crim_check_on and for crim_table. Thus upon refresh or reload or whatnot, it goes back to having the proper sequence executed.

The only thing I could do is from what I gather, is set a cookie to crim_on or crim_off. And then when I get the cookies I can run the proper set of on's and off's to turn things the way I want them. Only bad thing is that I end up setting 6 cookies on the persons computer. Is there a way I can store this in an array? And is there a way when everything works that it deletes the cookies? Cause when they refresh the page I want it to be back to default.

Thanks

Carnage

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 08-28-2001 02:29

Setting 6 cookies on someone's computer is not a big deal at all. The amount all the cookies allowed on one system is over a Gigabyte of data so six numbers and labels is hardly a problem. I'm not sure how an array would help but you could store a cookie with a bit map value. Something like "001000" for the third div turned on and the rest off. But you would need to parse that value. If you only have 6, I would just set one for each check. As far as getting rid of them when all is cool, all you need to do is set the expire date to a past date.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-28-2001 04:15

Keep in mind that browsers dont have to store more than 20 cookies for your server, and no more than 300 for the entire internet, so if you can put lots of data in a single cookie, it's generally a good idea to do so. You're allowed 4 kb per cookie.

Carnage
Nervous Wreck (II) Inmate

From:
Insane since: May 2001

posted posted 08-28-2001 06:54

Slime: is there a good place to figure out how to set these things using arrays?

« BackwardsOnwards »

Show Forum Drop Down Menu