SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Cookie Help

  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookie Help

    Hi everyone!

    Got a quick (cookie) question.
    I looked on the internet for some cookie scripts (redirect ones), but unfortunately haven't been too lucky with these.

    What I want to do is the following:

    When the cookie is not found it goes to my main page, for example:
    "www.anynamehere.com"
    On the main page I can select where I want to go -
    'Contacts', 'News', 'Forums' and so on. The main page has a drop down list with these options, each option has it's own link.

    When I click on 'continue' and go to the specific link (ex. www.anynamehere.com/contacts.html) it should remember my selection, so next time I log into www.anynamehere.com it's automatically will take me to contacts.html. I will not see the main page anymore.

    Now, if from the contacts page I select 'News' and go to www.anynamehere.com/news.html it has to remember that link as well. So if I close my browser and then reopen it, it should take me to www.anyname.com/news.html.

    I hope that makes sense.

    If anyone can give me any pointers I would greatly appreciate it. Maybe there is a script like that available online, I just wasn't lucky enough to find one.

    Thank you in advance!

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Florida
    Posts
    58
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    with php it would be as simple as:

    $_COOKIE["name"] = "/page/location.html";
    if($_COOKIE['name']){
    header("location: ".$_COOKIE['name']);
    }else{ header("location: /");}

    but the fact you use html makes it a bit harder, and jquery would be the best option (the number of devices without java enabled is amazing)

    after reading http://css-tricks.com/forums/discuss...nce-cookies/p1

    you may have to stick with php, and use .htaccess to rewrite your urls to accomadate the .html extension, or configure apache somehow to parse php inside of an html page

  3. #3
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you James,
    I'll look into it.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    If you do handle cookies from within JavaScript, these are the cookie handling functions that best do the job.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Jan 2012
    Location
    United States
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a very rough prototype I adapted from my book "JavaScript: Just the Basics." When you load Test.html, it looks for the FormerLocation cookie. If it finds it, it automatically navigates to that page. Test.html has a link to Test01.html. If you go to Test01.html, the script saves the current URL in the FormerLocation cookie. It saves the cookie for 1 hour, but you can change the lifetime to anything you want.
    Test.html:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Home Page</title>
    
    <script>
    
    var FormerLocation = getFormerLocationCookie ();
    
    if (FormerLocation)
       window.location = FormerLocation;
    
    function getFormerLocationCookie ()
      {
      /* get all cookies: */
      var allCookies = document.cookie;
    
      if (allCookies === "")
        return null;
    
      /* break up all-cookie string into an array: */
      var cookieList = allCookies.split ("; ");
    
      /* search through the array for the FormerLocation cookie: */
      for (var i = 0; i < cookieList.length; ++i)
        {
        var cookie = cookieList[i];
        var idx = cookie.indexOf ("=");
        /* extract cookie name: */
        var name = cookie.substring (0, idx);
        if (name === "FormerLocation")
          {
          /* correct cookie found: extract, decode, & return cookie value: */
          var value = cookie.substring (idx+1);
          return decodeURIComponent (value);
          }
        }
      return null;
      }
    
    </script>
    
    </head>
    <body>
    <h2>Home Page</h2>
    
    <a href="Test01.html">Go to Test01</a>
    
    </body>
    </html>
    Test01.html:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>JavaScript Tester Page</title>
    
    <script>
    
    /* for IE only: */
    var date = new Date();
    date.setHours (date.getHours()+1);
    
    document.cookie = "FormerLocation=" + encodeURIComponent (window.location) + "; max-age=3600; expires=" + date.toGMTString();
    
    </script>
    
    </head>
    <body>
    <h2>JavaScript Tester Page</h2>
    </body>
    </html>

  6. #6
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    If you do handle cookies from within JavaScript, these are the cookie handling functions that best do the job.
    I'd say there's room for improvement there, not least the encoding/decoding of the stored value.
    Tab-indentation is a crime against humanity.


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
  •