...

View Full Version : Pass the value of a cookie to a variable



marilynn.fowler
11-12-2010, 01:13 AM
I've avoided cookies for 12 years. I made my first cookie today:

<p><a href="projects.html" onclick="document.cookie='whichPic=8'">Set Cookie</a></p>


I have an external javascript sheet with an array of images w/text and the following code:


var current = 0;

function update() {
var pic = slides[current];
document.getElementById('proj_title').innerHTML = pic.title;
document.getElementById('proj_image').src = "images/slides/" + pic.image;
document.getElementById('proj_location').innerHTML = "<span>Location:</span> " + pic.location;
document.getElementById('proj_operation').innerHTML = "<span>Commercial Operation:</span> " + pic.commercial_operation;
document.getElementById('counter').innerHTML = (current + 1) + " of " + slides.length;

if(pic.link) {
document.getElementById('proj_link').style.display = "block";
document.getElementById('proj_href').href = pic.link;
} else {
document.getElementById('proj_link').style.display = "none";
}
}


When the user clicks the link, it should open the page to the slideshow and it should start on slide[8]. I think what I need to do is pass the value of the cookie to the variable "current", but I don't know how to do that. Also, I only need the cookie to last long enough for the new page to load (about 20 seconds), but all of the cookie scripts I've found set expiration in days.

Any guidance would be appreciated.

Old Pedant
11-12-2010, 01:27 AM
Search in this forum for cookie set/get code.

Philip, especially, has a "set piece" that he shows all the time. Nice and generic.

To set what is called a "session cookie", simply do *NOT* give any expiration time. A session cookie is held in the browser's memory and doesn't get stored in a cookie file. So when the browser is closed, it automatically goes away.

Dormilich
11-12-2010, 02:09 PM
A session cookie is held in the browser's memory and doesn't get stored in a cookie file. So when the browser is closed, it automatically goes away.

unless you change the default session cookie settings.

Logic Ali
11-12-2010, 03:28 PM
You can avoid cookies by using a querystring parameter.


<p><a href="projects.html?startSlide=4" >Slideshow from fifth pic</a></p>


Declare current like this:


var current = ( current = location.search.match( /(\?|\&(amp\;)*)startSlide=(\d+)/ ) ) ? current[ 3 ] : 0;

marilynn.fowler
11-12-2010, 06:09 PM
Logic Ali: I would like to use this, and understand what it means. How does the declaration work? What's the [ 3 ] : 0 mean?

Normally I would like the slideshow to start at [0], unless they click a photo on the homepage to see the featured project, the I would like to start it on [8]. If I set the variable to this equation, won't it always go to slide [8]? And right now I have an onload function that sets the starting slide to [0]. I know I should probably use an if...else statement, but how do I tell it "current =0 unless I click this link"?

Logic Ali
11-12-2010, 06:41 PM
Logic Ali: I would like to use this, and understand what it means. How does the declaration work? What's the [ 3 ] : 0 mean?It reads the querystring using a regex; the[3] refers to the third parenthesised match which will hold the numeric value if sent.


Normally I would like the slideshow to start at [0], unless they click a photo on the homepage to see the featured project, the I would like to start it on [8]. If I set the variable to this equation, won't it always go to slide [8]?No, if no parameter is sent, current will be 0.
And right now I have an onload function that sets the starting slide to [0]. I know I should probably use an if...else statement, but how do I tell it "current =0 unless I click this link"?You don't need that, it will be 0 if no parameter is sent.

marilynn.fowler
11-12-2010, 08:31 PM
I love this forum. Thank you all so much.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum