SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Store in value in cookie

    I'm new to using cookies. I am using JavaScript for a web project and I'm using cookies and I have 5 links for different movie trailers and I have a trailers.html page with a div with a id="movie" and want to know so that when you click on a Movie trailer link so that it stores a value in a cookie so that you can check on the trailers page which trailer was chosen and display in the div tag

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Allow me to introduce you to this AWESOME NEW TOOL for learning how to do anything.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by kieran82 View Post
    want to know so that when you click on a Movie trailer link so that it stores a value in a cookie so that you can check on the trailers page which trailer was chosen and display in the div tag
    These are the standard cookie-handling functions to create, read, and erase a cookie:

    Code javascript:
    function createCookie(name, value, days) {
    	var expires = '',
            date = new Date();
        if (days) {
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = '; expires=' + date.toGMTString();
        }
        document.cookie = name + '=' + value + expires + '; path=/';
    }
     
    function readCookie(name) {
        var cookies = document.cookie.split(';'),
            length = cookies.length,
            i,
            cookie,
            nameEQ = name + '=';
        for (i = 0; i < length; i += 1) {
            cookie = cookies[i];
            while (cookie.charAt(0) === ' ') {
                cookie = cookie.substring(1, cookie.length);
            }
            if (cookie.indexOf(nameEQ) === 0) {
                return cookie.substring(nameEQ.length, cookie.length);
            }
        }
        return null;
    }
     
    function eraseCookie(name) {
        createCookie(name, '', -1);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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