PDA

View Full Version : how write cookie (to manage a lightbox)



gridsystems
10-26-2009, 01:24 PM
HI,

I am quite new to javascript but with the help of jscheuer1's comment in this thread (http://www.dynamicdrive.com/forums/showpost.php?p=163248&postcount=15) I managed to get a Lightbox 2.04 working to display a single image when a page loads.

Basically I put this
<div><a id="lightboxPopImage" href="images/welcome_first_timer.jpg" rel="lightbox"></a> </div> at the end of the xhtml-code on my landing page, and then added two lines of code to the lightbox.js, so that it looks like this:

var th = this;
(function(){
var ids =
'overlay lightbox outerImageContainer imageContainer lightboxImage hoverNav prevLink nextLink loading loadingLink ' +
'imageDataContainer imageData imageDetails caption numberDisplay bottomNav bottomNavClose';
$w(ids).each(function(id){ th[id] = $(id); });
if (document.getElementById('lightboxPopImage')) {
th.start(document.getElementById('lightboxPopImage'));
}
}).defer();

},

This way I get a lightboxpopup every time the page loads.

But of course I would like to take it one step further, and use a cookie to control that this lightbox effect is only performed once per day. I have been battling with trying to implement a cookie solution all weekend but just have not got it to work (even started to look at PHP cookies in my desperation).

I think I just need a very simple routine inside the lightbox.js that checks for the existence of a cookie, and if it finds a cookie it skips the parts in the script where it shows the image, and just aborts the script altogether.

And then at the end of lightbox.js I guess I need to create the cookie in question (with a longevity of 1 day). At the end since I want to give the lightbox the chance to execute for a first time visitor before the cookie is created.

Is this difficult? Is it much to ask for help on this matter?

I would be very happy to get some pointers.
I enclose the full sized lightbox.js

Thanks a lot from Sweden!

jscheuer1
10-26-2009, 01:39 PM
See:

http://www.dynamicdrive.com/forums/showthread.php?p=163470#post163470

gridsystems
10-26-2009, 02:41 PM
John,

What an utterly slick adaptation! Really nice. The only downside to it is that I did not get the chance of learning more javascript by solving the problem with the help of someone, but heck I can't complain at all. Now I have more time to read those two books I bought from scratch... :)

Thanks again for your reply and your smooth extension of the lightbox.

/Peter

jscheuer1
10-26-2009, 02:58 PM
If you want more on just cookies see:

http://www.dynamicdrive.com/forums/blog.php?b=32

And for a bit more of an explanation of how they work (but less efficient code):

http://www.quirksmode.org/js/cookies.html