Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    San Francisco
    Posts
    455
    Thanks
    19
    Thanked 15 Times in 15 Posts

    Pass the value of a cookie to a variable

    I've avoided cookies for 12 years. I made my first cookie today:
    Code:
    <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:

    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.
    Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by Old Pedant View Post
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    You can avoid cookies by using a querystring parameter.

    Code:
    <p><a href="projects.html?startSlide=4" >Slideshow from fifth pic</a></p>
    Declare current like this:

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

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    San Francisco
    Posts
    455
    Thanks
    19
    Thanked 15 Times in 15 Posts
    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"?
    Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx

  • #6
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by marilynn.fowler View Post
    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.

  • Users who have thanked Logic Ali for this post:

    marilynn.fowler (11-12-2010)

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    San Francisco
    Posts
    455
    Thanks
    19
    Thanked 15 Times in 15 Posts
    I love this forum. Thank you all so much.
    Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read. Groucho Marx


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •