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 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help checking for cookies...

    Again I'm working through this old javascript book a friend lended me trying to learn some of this on my own...and I've ran across a stumbling block with cookies.

    I want to create a function that checks to see if a cookie exists... and if so, split it in individual parts and display then through an alert()

    can someone help me out... I'm not really even sure where to begin

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,466
    Thanks
    3
    Thanked 495 Times in 482 Posts
    you have not replied to the last reply I made to one of your posts

    and cookies should be addressed after the basics have been mastered

    but I just posted this on another forum


    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"
    >

    <
    html>

    <
    head>
      <
    title></title>

    <
    script language="JavaScript" type="text/javascript">
    <!--
    var 
    CookieAry=new Array('tom1','bill0','dick','harry','bill','tomee');
    var 
    FindAry=new Array();
    var 
    v;

    function 
    FindCookieValue(){
     if (!
    pv_GetCookie()){ return; }
     
    v=pv_GetCookie();
     for (
    i=0;i<CookieAry.length;i++){
      if (
    CookieAry[i].match(v)){
       
    FindAry[FindAry.length]=CookieAry[i];
      }
     }
     
    alert('Found '+FindAry);
    }

    var 
    pv_exp=new Date(new Date().getTime()+.1*86400000);
    var 
    pv_Cookie='VicX';

    function 
    pv_GetCookie(){
     var 
    pv_st=document.cookie.indexOf(pv_Cookie+"=");
     var 
    pv_len=pv_st+pv_Cookie.length+1;
     if ((!
    pv_st)&&(pv_Cookie != document.cookie.substring(0,pv_Cookie.length))) return null;
     if (
    pv_st==-1) return null;
     var 
    pv_end=document.cookie.indexOf(";",pv_len);
     if (
    pv_end==-1pv_end=document.cookie.length;
     return 
    unescape(document.cookie.substring(pv_len,pv_end));
    }


    function 
    pv_SetCookie(value,expires) {
     
    document.cookie=pv_Cookie+"="+value+";expires="+expires+";path=/;"
    }


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

    <body onload="FindCookieValue();" onunload="pv_SetCookie('tom',pv_exp);" >
    <input id=h name="" size="10">
    </body>

    </html> 

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what msg did I not reply to...

    actually I've been moving along pretty good in the book and I'm not on cookies...

    what does the code you posted do exactly?

  • #4
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help

    ok here's what I have so far... but I'm getting an error. What am I missing?

    <HTML>
    <HEAD>
    <TITLE>Cookies</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    function showCookie() {
    if(GetCookie("name") == null) {
    alert("No cookies found!");
    }

    // else this is second or greater visit
    else {
    document.cookie = encodeURI("name=" + document.cookieExample.name.value);
    document.cookie = encodeURI("date=" + document.cookieExample.date.value);
    var savedData = decodeURI(document.cookie);
    var dataArray = savedData.split("; ");
    var messageString = "";
    for (var i = 0; i < dataArray.length; ++i) {
    messageString += dataArray[i] + "\n";
    }
    alert(messageString);
    return false;
    }
    }

    // STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <H3>Cookies</H3>
    <FORM NAME="cookieExample" onReset="return confirmReset();" onSubmit="return showCookie();">
    <P><B>Name</B><BR>
    <INPUT TYPE="text" NAME="name" SIZE=50></P>
    <P><B>Date</B><BR>
    <INPUT TYPE="text" NAME="date" SIZE=50></P>
    <P><INPUT TYPE="reset">
    <INPUT TYPE="submit"></P>
    </FORM>
    </BODY>
    </HTML>
    Last edited by disneyfan; 03-15-2005 at 12:27 PM.

  • #5
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe you should post the exact error you are getting?

  • #7
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok...

    object expected on line 7 which is

    if(GetCookie("name") == null) {

  • #8
    New Coder
    Join Date
    Mar 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    GetCookie is not a javascript function AFAIK.
    I believe you can access cookies through document.cookie

  • #9
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you mean like this?

    if(document.cookie("name") == null) {

    I don't think this is working either... any other ideas?

  • #10
    New Coder
    Join Date
    Mar 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    document.cookie isn't an array, so what you're doing is absolutely incorrect, you're correct in that.

    document.cookie is the raw cookie data. You need to process is into something useful. I suggest you search google or maybe try an "alert(document.cookie);" to see how it's buildup.

    Edit: I believe you are trying to use the cookie function declare here: http://www.netspade.com/articles/javascript/cookies.xml

    The functions look interesting, but of course you shouldn't forget to include those functions in your page if you intent to use them.


  •  

    Posting Permissions

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