setting local cookie

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • socialism001@yahoo.com

    #1

    setting local cookie

    I'm trying to store a value in a cookie but its not working. Can
    anyone see what I might be doing wrong.

    Thanks,
    Chris
    ~~~~~~~~~~~~~~~ ~~~
    <script language="javas cript">
    if(document.coo kie.indexOf("be enHere1=true")! =-1)

    else
    {
    var ExpirationDate = new Date();
    var oneDayFromNow = exp.getTime() + (1 * 24 * 60 * 60 * 1000);
    exp.setTime(one DayFromNow);
    document.cookie = "beenHere1=true ; ";
    document.cookie = document.cookie + "stnum=" +
    $query->param('-stnum'); + "; ";
    document.cookie = document.cookie + "stnm=" +
    $query->param('~stnm') ; + "; expires=" + exp.toGMTString ();
    }

    </script>

  • McKirahan

    #2
    Re: setting local cookie

    <socialism001@y ahoo.com> wrote in message
    news:1106690406 .206491.286160@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    > I'm trying to store a value in a cookie but its not working. Can
    > anyone see what I might be doing wrong.
    >
    > Thanks,
    > Chris
    > ~~~~~~~~~~~~~~~ ~~~
    > <script language="javas cript">
    > if(document.coo kie.indexOf("be enHere1=true")! =-1)
    >
    > else
    > {
    > var ExpirationDate = new Date();
    > var oneDayFromNow = exp.getTime() + (1 * 24 * 60 * 60 * 1000);
    > exp.setTime(one DayFromNow);
    > document.cookie = "beenHere1=true ; ";
    > document.cookie = document.cookie + "stnum=" +
    > $query->param('-stnum'); + "; ";
    > document.cookie = document.cookie + "stnm=" +
    > $query->param('~stnm') ; + "; expires=" + exp.toGMTString ();
    > }
    >
    > </script>
    >[/color]

    Where is "exp" declared?

    Are you using PHP inside JavaScript?

    What does the following do?
    $query->param('-stnum');
    $query->param('~stnm') ;

    You should build the value of a variable and set "document.cooki e = " once.

    For example,

    <script type="text/javascript">
    if (document.cooki e.indexOf("been Here1=true") < 0) {
    var now = new Date();
    var exp = new Date(now.getTim e() + (1 * 24 * 60 * 60 * 1000));
    document.cookie = "beenHere1=true !;expires=" + exp.toGMTString ();
    }
    </script>


    Comment

    • Robert

      #3
      Re: setting local cookie

      In article <1106690406.206 491.286160@z14g 2000cwz.googleg roups.com>,
      socialism001@ya hoo.com wrote:
      [color=blue]
      > I'm trying to store a value in a cookie but its not working. Can
      > anyone see what I might be doing wrong.[/color]

      Here is a copy of my earlier post. Pick a working cookie routine.

      Robert

      Here are my suggestions.

      1) Always set the expiration time. IE works better.

      2) You may have disabled cookies. In IE, I had to go to the site then
      check whether or not cookies were enabled.

      3) Maybe your cookie rountines could use improvements.

      I suggest using an existing set of cookie routines. The
      document.cookie can contain existing cookies and you need to preserve
      them. ( Don't know why such routines aren't shipped with the browsers.
      )

      ~kaeli~ has developed some cookie code. You may view it in this
      article:

      http://groups.google.com/groups?hl=e...SO-8859-1&selm...
      ..1af96c2a4f8ab c94989dcc%40nnt p.lucent.com

      Also, ~kaeli~ reports that:
      There's some even better code that sets more cookie properties like
      domain, secure, path, etc here.


      4) I wrote a short program for verifying cookies will work on a site.
      The cookie code was taken from:
      Functions from Using HTML 4, Java 1.1, and JavaScript 1.2:
      Complete. Definitive. Invaluble.
      by Eric Jim O'Donnell, et al.
      and from the Netscape develper's site. Here is my cookie testing
      code:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html;
      charset=windows-1252">
      <title>Rememb er by cookie</title>

      <script type="text/javascript">

      // Sets cookie values. Expiration date is optional
      //
      function setCookie(name, value, expire) {
      document.cookie = name + "=" + escape(value)
      + ((expire == null) ? "" : ("; expires=" + expire.toGMTStr ing()))

      }

      function getCookie(Name) {
      var search = Name + "="
      if (document.cooki e.length > 0) { // if there are any cookies
      offset = document.cookie .indexOf(search )
      if (offset != -1) { // if cookie exists
      offset += search.length
      // set index of beginning of value
      end = document.cookie .indexOf(";", offset)
      // set index of end of cookie value
      if (end == -1)
      end = document.cookie .length
      return unescape(docume nt.cookie.subst ring(offset, end))
      }
      }

      }

      function deleteCookie(na me,path,domain) {
      if (getCookie(name )) document.cookie = name + "=" +
      ( (path) ? ";path=" + path : "") +
      ( (domain) ? ";domain=" + domain : "") +
      ";expires=T hu, 01-Jan-70 00:00:01 GMT";

      }

      function registerCookie( cookieName,cook ieValue) {
      var today = new Date()
      var expires = new Date()
      expires.setTime (today.getTime( ) + 1000*60*60*24*3 65)
      setCookie(cooki eName, cookieValue, expires)
      }

      </script>

      <body>
      <p>Let's see if we can set then read a cookie

      <script type="text/javascript">
      document.write( "when on (document.url) " + document.URL);
      document.write( "</p>");

      var all = "I hope this cookie doesn't crumble.";

      document.write( "<p>");
      document.write( "document.cooki e before deleteCookie = "
      + document.cookie + "<br><br>") ;
      deleteCookie("m ycookietry");
      document.write( "document.cooki e after deleteCookie= "
      + document.cookie + "<br><br>") ;

      registerCookie( "mycookietry",a ll);

      document.write( "document.cooki e after registerCookie = "
      + document.cookie + "<br><br>") ;

      theMessage = getCookie("myco okietry");
      document.write( "theMessage = " + theMessage + "<br>");
      document.write( "</p>");
      </SCRIPT>
      </body>
      </html>

      Comment

      • Dr John Stockton

        #4
        Re: setting local cookie

        JRS: In article <lNidncW1Ody_BG rcRVn-jw@comcast.com> , dated Wed, 26 Jan
        2005 07:22:08, seen in news:comp.lang. javascript, McKirahan
        <News@McKirahan .com> posted :[color=blue]
        >
        > var now = new Date();
        > var exp = new Date(now.getTim e() + (1 * 24 * 60 * 60 * 1000));
        > document.cookie = "beenHere1=true !;expires=" + exp.toGMTString ();[/color]


        var D = new Date();
        D.setDate(D.get Date()+1)
        document.cookie = "beenHere1=true !;expires=" + D.toGMTString() ;

        is smarter; if a day is called for, give a civil day, so that it expires
        at the same time next day.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • Dr John Stockton

          #5
          Re: setting local cookie

          JRS: In article <rccharles-6D0C07.13004026 012005@individu al.net>, dated
          Wed, 26 Jan 2005 13:00:41, seen in news:comp.lang. javascript, Robert
          <rccharles@my-deja.com> posted :[color=blue]
          >
          >function registerCookie( cookieName,cook ieValue) {
          > var today = new Date()
          > var expires = new Date()
          > expires.setTime (today.getTime( ) + 1000*60*60*24*3 65)
          > setCookie(cooki eName, cookieValue, expires)
          >}[/color]


          function registerCookie( cookieName,cook ieValue) {
          var D = new Date()
          D.setMonth(D.ge tMonth()+12)
          setCookie(cooki eName, cookieValue, D)
          }

          is smarter; if a year is called for, give 12 months, and it will expire
          at the same date next year regardless of the Leap Year situation -
          except when called on Feb 29th, of course, when it gives Mar 1st.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
          Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
          PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
          Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

          Comment

          Working...