SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Nice Mug jer's Avatar
    Join Date
    Apr 2000
    Location
    Canada
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I'm trying to go through the WebMonkey cookies tutorial but I can't find the information I need when it comes to setting(and reading) complicated cookies.

    Right now, I can set and read a basic cookie with a single piece of information.

    Here's what I have so far:

    Set The Cookie:
    -----------------

    <script>
    function setCookie()
    {

    var
    the_name = prompt("Enter a URL in the form: www.yahoo.com","");

    var the_cookie = "javascript=username:" + escape(the_name);

    document.cookie = the_cookie;

    alert("Now go to the next page.");

    }


    </script>
    <a href="#" onClick="setCookie(); return false;">set the cookie</a>
    ----------------------

    Ok, now for READING the cookie:

    ---------------------

    <script language="JavaScript">
    <!-- hide me

    function readCookie(name) {

    // if there's no cookie, return false else get the value and return it

    if(document.cookie == '') return false;
    else return unescape(getCookieValue(name));

    }



    function getCookieValue(name)
    {
    // Declare variables.
    var firstChar, lastChar;

    // Get the entire cookie string. (This may have other name=value pairs in it.)
    var theBigCookie = document.cookie;

    // Grab just this cookie from theBigCookie string.

    // Find the start of 'name'.
    firstChar = theBigCookie.indexOf(name);

    // If you found it,
    if(firstChar != -1)
    {
    // skip 'name' and '='.
    firstChar += name.length + 1;

    // Find the end of the value string (i.e. the next ';').
    lastChar = theBigCookie.indexOf(';', firstChar);

    if(lastChar == -1) lastChar = theBigCookie.length;

    // Return the value.
    return theBigCookie.substring(firstChar, lastChar);

    } else {
    // If there was no cookie, return false.
    return false;
    }
    }



    // show me-->
    </script>

    <script>
    var the_cookie = readCookie("javascript");
    var broken_cookie = the_cookie.split(":");
    var the_name_part = broken_cookie[1];
    var broken_name = the_name_part.split(";");
    var the_name = broken_name[0];
    document.write('<a href=http://'+the_name+'>'+the_name+'</a>');

    </script>
    ---------------------

    Simple enough right? Well, now there's ONE more thing I need to know how to do.. and that's adding more information to the cookie. I need one more piece of information added to this cookie, it would be link_name.

    This would allow people to set the link they want, and then when it is displayed, they could click on the link_name to go to their link.

    What would I need to add to my code to be able to do that?

    thanks a TON to anyone who can help me

    -Chuck
    Jeremy Watts :: Co-Founder
    Modevia Web Services :: Experience the Difference.

  2. #2
    Nice Mug jer's Avatar
    Join Date
    Apr 2000
    Location
    Canada
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mods? Experts? Anyone? For some reason I'm finding it hard to believe nobody knows how to do what I asked in the above post.

    I just need to be able to store/retrieve more seperate pieces of information in the cookie. How do I do that?

    Any help is greatly apprecaited, as always

    -Chuck
    Jeremy Watts :: Co-Founder
    Modevia Web Services :: Experience the Difference.

  3. #3
    ********* Addict DLG_1's Avatar
    Join Date
    Jun 2000
    Location
    Texas, USA
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to save one cookie, that is:

    document.cookie = "cookiename=" + cookieValue

    to save multiple variables under that one cookie you must delimit them.

    that is:

    document.cookie = "cookiename=" + cookieValue
    + "/" + "cookiename2" + cookieValue2
    + "/" + "cookiename3" + cookieValue3
    + "/" + "cookiename4" + cookieValue4
    + "/" + "cookiename5" + cookieValue5
    + "/" + "cookiename6" + cookieValue6
    :
    :
    yadda yadda

    to keep them persistent, that is so that they don't expire when the user closes the browser then you must have
    :
    :
    + "; expires=" + "01-07-01"

    so when you read the cookie, you must search for the "cookiename=" or the "cookiename2" and then split the cookie using "/" and enter that info into a two element array.

    do i make any sense?

    btw, you will not find too much info on cookies, I struggled so much, but I have it under my belt now.

    I'll try to help more when i make it back into the office.

    Regards,

    Sam

  4. #4
    ********* Addict DLG_1's Avatar
    Join Date
    Jun 2000
    Location
    Texas, USA
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you still need help?

  5. #5
    Nice Mug jer's Avatar
    Join Date
    Apr 2000
    Location
    Canada
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes actually

    I've been incredibly busy with school so haven't had much of a chance to work on the site. I don't really know what extra lines I need to add to my original code, and to the code that reads the cookie.

    I pretty much just need to be able to output the same thing as before:

    document.write('<a href=http://'+the_name+'>'+the_name+'</a>');

    But I'd like it to have multiple variables so it would write something to the extent of:

    document.write('<a href=http://'+the_url+'>'+the_name+'</a>');

    I'd need this setup for a few variables so the names would be something like:

    the_url_1
    the_name_1
    the_url_2
    the_name_2

    and so on.

    I'm not really sure how to use arrays to organize this information.

    Any further help you could give me would be apprecaited

    thanks a lot!

    -Chuck

  6. #6
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What's preventing you from using two cookies -- one for the link text and one for the url -- to store the information you need?
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  7. #7
    Nice Mug jer's Avatar
    Join Date
    Apr 2000
    Location
    Canada
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I don't exactly know how to do that either. I figured that using one cookie to store it all would, in the long run, result in a smaller, more easily manageable script than if I had multiple cookies.

    I thought of using a different cookie for each link, but then I've got to store multiple variables while still using multiple cookies.

    I'm not really sure which would be easiest to do.

    Any advice would be helpful

    thanks,

    -chuck
    Jeremy Watts :: Co-Founder
    Modevia Web Services :: Experience the Difference.

  8. #8
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try reading my quick tutorial on Cookies -- it might help clarify what's involved in each of your options, and help you decide for yourself what to do.

    Persistent Data without CGI Part II - An Introduction to Cookies
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  9. #9
    Nice Mug jer's Avatar
    Join Date
    Apr 2000
    Location
    Canada
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I think I've FINALLY figured out how to create more than one cookie. What I want to do is allow people to create custom links on the homepage. However, this presents a problem for first time (and non-participating) visitors. I know how I could set a default cookie value, but I don't know how to prevent it from going back to the default value everytime someone visits the page

    -chuck

  10. #10
    ********* Addict DLG_1's Avatar
    Join Date
    Jun 2000
    Location
    Texas, USA
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay, i'm assuming you have the cookie thing working, but what do you mean about default values? Please explain in greater detail what you want to accomplish...



    Sam


  11. #11
    Nice Mug jer's Avatar
    Join Date
    Apr 2000
    Location
    Canada
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, it's the same idea as the "customizable bookmarks" on the WebDevPortal.com website (Im not sure how they do it, they have too much code for me to sift through).

    Here's what I want:

    On the first visit to the site, the "Bookmarks" links are set to default values (say, to Yahoo, SitePoint, and ActiveWM.com).

    They then can proceed to Customize the links, by setting the URLs and LinkNames to whatever they like. After setting these values, everytime they visit the homepage, they see the ones they set.

    The problem is making it so there are default values for those who havent customized the links yet, but also so that those default values aren't overwriting the cookies they set.

    Perhaps what I need is a script that checks for that cookie, and if there is no cookie, write the default value. I'm just not sure how to go about doing that...

    -chuck

  12. #12
    Nice Mug jer's Avatar
    Join Date
    Apr 2000
    Location
    Canada
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out

    Thanks a ton to all who helped me

    -chuck


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
  •