Javascript cookies?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bert

    #1

    Javascript cookies?

    I have been reading the post and the FAQ and have been unable to find
    anything that will help with my problem.

    First let me say that I am not a web developer, designer and no next to
    nothing about javascript. I am just rebuilding my site and need help. We run
    a bamboo nursery and the site has listings of over 100 varieties we sell.
    The site uses regular pages and framesets for the discriptions of the
    bamboo. The framesets have a top frame, left frame and main frame.

    The left frame has a listing of all our varities which can be sorted in
    several ways (height, diam. etc). Clicking on a variety loads the
    description for the variety in the main frame. All of these files are
    located in a directory called bamboo-frames.

    We also have frames sets with the same layout that gives the user the
    opprotunity to select a sub set of varieties by use. These files are located
    in a directory called bamboo-uses but the lelt frame calls the description
    page from the bamboo-frames directory.

    The main frame for both the above sorts has the following starting mainframe
    code:

    Head
    <script language=javasc ript
    src="../bamboo-cookies/bamboo-cookies.js"></script>

    body

    <SCRIPT LANGUAGE="JavaS cript">
    <!--
    var pic = getCookie("size pic");
    if (!pic) {
    pic = "Full size";
    setCookie('size pic', pic);
    }
    // -->
    </SCRIPT>

    My html code

    <form>
    <input name="" type="button" onClick="setCoo kie('sizepic',' Full size') "
    value="Full size">
    <input name="" type="button" onClick="setCoo kie('sizepic',' Half size')"
    value="Half size">
    <input name="" type="button" onClick="alert( getCookie('size pic'))"
    value="Current size">
    </form>

    The functions were downloaded and pasted into bamboo-cookies.js

    Clicking on the alert button will give the correct results regardless of the
    starting main frame.

    On the description pages if have the following code:

    head
    <script language=javasc ript
    src="../bamboo-cookies/bamboo-cookies.js"></script>

    body
    My html

    <SCRIPT LANGUAGE="JavaS cript">
    <!--
    var pic = getCookie("size pic");
    switch(pic)
    {
    case "Full size":document. write("<img src='../bamboo-photos/bamboo-0105.jpg'
    alt='Bamboo - P. bambusoides tanakae' width='480' height='640'
    hspace='10'>");
    ; break
    case "Half size": document.write( "<img
    src='../bamboo-photos50/bamboo-0105.jpg' alt='Bamboo - P. bambusoides
    tanakae' width='240' height='320' hspace='10'>");
    ; break
    default: document.write( "<img src='../bamboo-photos/bamboo-0105.jpg'
    alt='Bamboo - P. bambusoides tanakae' width='480' height='640'
    hspace='10'>");
    ; break
    }
    //-->
    </SCRIPT>

    When calling the description page from the bamboo-frames directory the
    correct size photo is download with the page.

    When calling the description page from the bamboo-uses directory the last
    size called from the bamboo-frames directory is used even though the buttons
    apparently change the cookie value.

    In the comments of the setCookie function it says the default directory is
    path=/ so I do not pass a value.

    I don't understand all that is happening but hope someone can see what I am
    doing wrong. I have been trying to get this to work for 4 days now and am
    really frustrated by my lack of knowledge.

    Any guidance appreciated.

    Bert



  • Bert

    #2
    Re: Javascript cookies?


    "Bert" <bert_remove@me ckcom.net> wrote in message
    news:10f38gq3jg 0517e@corp.supe rnews.com...[color=blue]
    > I have been reading the post and the FAQ and have been unable to find
    > anything that will help with my problem.[/color]
    (snip)
    [color=blue]
    > I don't understand all that is happening but hope someone can see what I[/color]
    am[color=blue]
    > doing wrong. I have been trying to get this to work for 4 days now and am
    > really frustrated by my lack of knowledge.
    >
    > Any guidance appreciated.
    >
    > Bert[/color]

    On further searching I have found the following:

    I have added a expire time- did not correct problem

    The buttons on the bamboo-uses frameset main page return the value of the
    cookie, however when I call the
    bamboo-frames/description-mainframe page I get a null when the getCookie
    function is called and the switch default is executed.

    Maybe this will help in recommending a solution.

    Bert



    Comment

    • Robert

      #3
      Re: Javascript cookies?

      In article <10f3h9ktcvicl6 6@corp.supernew s.com>,
      "Bert" <bert_remove@me ckcom.net> wrote:
      [color=blue]
      > "Bert" <bert_remove@me ckcom.net> wrote in message
      > news:10f38gq3jg 0517e@corp.supe rnews.com...[color=green]
      > > I have been reading the post and the FAQ and have been unable to find
      > > anything that will help with my problem.[/color]
      > (snip)
      >[color=green]
      > > I don't understand all that is happening but hope someone can see what I[/color]
      > am[color=green]
      > > doing wrong. I have been trying to get this to work for 4 days now and am
      > > really frustrated by my lack of knowledge.
      > >
      > > Any guidance appreciated.
      > >
      > > Bert[/color]
      >
      > On further searching I have found the following:
      >
      > I have added a expire time- did not correct problem[/color]

      Best to set the expiration time. Sometimes IE gets confused.[color=blue]
      >
      > The buttons on the bamboo-uses frameset main page return the value of the
      > cookie, however when I call the
      > bamboo-frames/description-mainframe page I get a null when the getCookie
      > function is called and the switch default is executed.
      >
      > Maybe this will help in recommending a solution.
      >
      > Bert[/color]

      Maybe you are hitting some security restriction.

      When you do not specify a path parameter, your client needs to load all
      the html file from the same directory or any subdirectory of the
      directory.

      See the path parameter and domain parameter to relax what html pages can
      read the cookie.

      Did this code work before? What was changed just before the code
      stopped working?

      Could you give a URL?

      Robert

      Comment

      • Robert

        #4
        Re: Javascript cookies?

        In article <10f3h9ktcvicl6 6@corp.supernew s.com>,
        "Bert" <bert_remove@me ckcom.net> wrote:

        Codes are more difficult than they appear.

        Try the page in Netscape 7.1 (gekco). In Netscape 7.1 you can use the
        cookie manager to see your cookies. This way you can verify that a
        cookie has been set. You can turn on privacy and security -> cookies ->
        "ask me before storing a cookie" to see when a cookie is stored.

        You can see your cookie files in IE too, but it isn't as easy as
        Netscape imho.

        Robert

        Comment

        • Bert

          #5
          Re: Javascript cookies?

          "Robert" <rccharles@my-deja.com> wrote in message
          news:rccharles-C00A92.13272812 072004@news4.we st.earthlink.ne t...[color=blue]
          > In article <10f3h9ktcvicl6 6@corp.supernew s.com>,
          > "Bert" <bert_remove@me ckcom.net> wrote:
          >[color=green]
          > > "Bert" <bert_remove@me ckcom.net> wrote in message
          > > news:10f38gq3jg 0517e@corp.supe rnews.com...[color=darkred]
          > > > I have been reading the post and the FAQ and have been unable to find
          > > > anything that will help with my problem.[/color]
          > > (snip)
          > >[color=darkred]
          > > > I don't understand all that is happening but hope someone can see what[/color][/color][/color]
          I[color=blue][color=green]
          > > am[color=darkred]
          > > > doing wrong. I have been trying to get this to work for 4 days now and[/color][/color][/color]
          am[color=blue][color=green][color=darkred]
          > > > really frustrated by my lack of knowledge.
          > > >
          > > > Any guidance appreciated.
          > > >
          > > > Bert[/color]
          > >
          > > On further searching I have found the following:
          > >
          > > I have added a expire time- did not correct problem[/color]
          >
          > Best to set the expiration time. Sometimes IE gets confused.[color=green]
          > >
          > > The buttons on the bamboo-uses frameset main page return the value of[/color][/color]
          the[color=blue][color=green]
          > > cookie, however when I call the
          > > bamboo-frames/description-mainframe page I get a null when the getCookie
          > > function is called and the switch default is executed.
          > >
          > > Maybe this will help in recommending a solution.
          > >
          > > Bert[/color]
          >
          > Maybe you are hitting some security restriction.
          >
          > When you do not specify a path parameter, your client needs to load all
          > the html file from the same directory or any subdirectory of the
          > directory.
          >
          > See the path parameter and domain parameter to relax what html pages can
          > read the cookie.
          >
          > Did this code work before? What was changed just before the code
          > stopped working?
          >
          > Could you give a URL?
          >
          > Robert[/color]

          Thanks Robert,

          I am still working on the site on my local machine so no url yet.

          The code never worked I am just trying to get it to.

          Thanks for the advice and suggestions. I will try NetScape to see if I can
          find anything when I get time. I have decided not to use the code in the
          USES directory until I have time to try to learn enough to understand what
          is happening.

          Again thanks and I will be back later when I try to get it working.

          I also want to express my thanks to all the folks that post here What I have
          learned so far is from reading the post.

          Bert


          Comment

          • Robert

            #6
            Re: Javascript cookies?

            Robert <rccharles@my-deja.com> wrote in message news:<rccharles-45C12C.13325612 072004@news4.we st.earthlink.ne t>...[color=blue]
            > In article <10f3h9ktcvicl6 6@corp.supernew s.com>,
            > "Bert" <bert_remove@me ckcom.net> wrote:
            >
            > Codes are more difficult than they appear.
            >[/color]
            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:


            ..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

            • Bert

              #7
              Re: Javascript cookies?


              "Robert" <rccharles@my-deja.com> wrote in message
              news:c6bb75ff.0 407140720.7fe93 33b@posting.goo gle.com...[color=blue]
              > Robert <rccharles@my-deja.com> wrote in message[/color]
              news:<rccharles-45C12C.13325612 072004@news4.we st.earthlink.ne t>...[color=blue][color=green]
              > > In article <10f3h9ktcvicl6 6@corp.supernew s.com>,
              > > "Bert" <bert_remove@me ckcom.net> wrote:
              > >
              > > Codes are more difficult than they appear.
              > >[/color]
              > 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...859-1&selm=MPG
              > .1af96c2a4f8abc 94989dcc%40nntp .lucent.com
              >
              > Also, ~kaeli~ reports that:
              > There's some even better code that sets more cookie properties like
              > domain, secure, path, etc here.
              > http://www.acm.uiuc.edu/webmonkeys/j...ipt/cookies.js
              >
              >
              >
              > 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>[/color]

              Thanks Robert. Now I am on my next problem.

              Bert


              Comment

              • Dr John Stockton

                #8
                Re: Javascript cookies?

                JRS: In article <10fgmvkg7t52aa e@corp.supernew s.com>, seen in
                news:comp.lang. javascript, Bert <bert_remove@me ckcom.net> posted at Fri,
                16 Jul 2004 18:51:29 :
                [color=blue]
                >Lines: 131[/color]

                [color=blue][color=green]
                >> 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][/color]

                The number 1000*60*60*24 is more economically written 864e5.

                I see no need for having today as well as expires.

                expires.setMont h(expires.getMo nth()+12)

                is simpler, and gets to the present time & date a year ahead (if
                possible; else reasonable substitute). Note the avoidance of
                getYear/getFullYear. If you really want 365 days,

                expires.setDate (expires.getDat e()+365)

                with the same caveat about the time only.

                [color=blue][color=green]
                >> </html>[/color]
                >
                >Thanks Robert. Now I am on my next problem.[/color]

                Please do not over-quote.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> JL / RC : FAQ for 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

                Working...