Jump to content


Help with storing cookies


6 replies to this topic

#1 chaoslight

    Young Padawan

  • Members
  • Pip
  • 220 posts
  • Gender:Male
  • Interests:Computer, coding, designing...

Posted 23 September 2006 - 07:59 AM

Hey, I was wondering if anyone could help me with this code.

I've got this code on my site, which I found on sitepoint..

function MinMax(id,n,x){
 obj=document.getElementById(id);
 if (obj.offsetHeight<x){
  obj.style.height=x+'px';
 }
 else {
  obj.style.height=n+'px';
 }
}

And then on the page I have..

<a onclick="MinMax('Aff',0,80);">
<img src='link/here'>
</a>
<div id='Aff'>
content
</div>

You see.. on my site I have a maximize and minimize button on each div.. or I'm hoping to. But I want it so that if a member minimizes a div, and then leaves and comes back later, the div is still minimized. Could someone help me? Oh and I have no Javascript experience whatsoever.. so it would be nice if you could just give me the code..

Thanks in advance..

Edited by chaoslight, 23 September 2006 - 07:59 AM.


#2 Copernicus

    Young Padawan

  • Members
  • Pip
  • 32 posts

Posted 23 September 2006 - 10:58 AM

I would set a cookie for the status of that div, 1 to open and 0 to off, using php...they minimize it, it sets it to 0, they maximize it, it sets to 1.

then show the div minimized or maximized based on the cookie value...

#3 chaoslight

    Young Padawan

  • Members
  • Pip
  • 220 posts
  • Gender:Male
  • Interests:Computer, coding, designing...

Posted 23 September 2006 - 11:16 AM

ahh thanks.. I'll try that :)

Edit:

I'm sorry.. I have no idea how to go about this.. is there anyway to do it in javascript and intergrate it into the code given in the first post?

Edited by chaoslight, 23 September 2006 - 12:13 PM.


#4 chaoslight

    Young Padawan

  • Members
  • Pip
  • 220 posts
  • Gender:Male
  • Interests:Computer, coding, designing...

Posted 23 September 2006 - 05:02 PM

Anybody? I'd like to get it other and done with..

btw Sorry for the double post..

#5 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 23 September 2006 - 07:14 PM

I don't know anything about JavaScript and cookies, I don't even know if it is able to work with them or anything, but I would use simply AJAX.
When the user calls that function, have it use Prototype (easiest way of using anything AJAX, even for a beginner like me) and send a request to a PHP script that will add the cookie with the value (1 or 0, maximized or minimized). Then, when the page is loaded, you can have PHP generate the height of the div dependant on the cookie; if it is 1, make the height 80px, else make it 0px.

Quite simple really. :)


function MinMax(id,n,x){
obj=document.getElementById(id);
if (obj.offsetHeight<x){
  obj.style.height=x+'px';
objectStatus = 1;
}
else {
  obj.style.height=n+'px';
objectStatus = 0;
}

var opt = {
	method:'post',
	postBody:'m=objectStatus&status='+objectStatus,
	onSuccess: function(t) {},
	onLoading: function() {}
	}
	// Send request
	new Ajax.Request('/ajax.php', opt);
}
I might have the objectStatus' mixed up, one might be 1 and the other 0, can't quite tell what variable is suppsed to be what.

But you'd then have your AJAX handling PHP script like this.
<?php
if($_POST['m'] == 'objectStatus'){
setcookie('objectStatus', $_POST['status'], time()+2592000, '/');
}
?>
Then, you'd have with the div something like...
<a href="java script:MinMax('Aff',0,80);">
<img src='link/here'>
</a>
<div id='Aff' style="height:<?php echo ($_COOKIE['objectStatus'] == 1) ? '80px' : '0px'; ?>">
content
</div>

Edited by Demonslay, 23 September 2006 - 07:23 PM.


#6 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 23 September 2006 - 08:40 PM

Wow, you call yourselves programmers? Try google sometimes....

http://www.quirksmod...js/cookies.html

Read up on that and it will provide enough information to do your JS cookie thingy.

#7 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 24 September 2006 - 11:17 AM

I didn't have time for Google just then, I was caught up in other work. >.<

I honestly didn't think JavaScript had access to cookies...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users