Web Analytics Made Easy -
StatCounter cookies (no, not oreos) - CodingForum

Announcement

Collapse
No announcement yet.

cookies (no, not oreos)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • cookies (no, not oreos)

    Hi,

    I need a script that writes one cookie on a visitor's computer upon the page loading (index.html)

    Then I need a second script to put on a separate page that tries to read the cookie, if it does find it, it goes to page1.html, if it doesn't, it goes to page2.html. Is anyone out there good enough with cookies to figure this out?

    Thanks

  • #2
    Hi,

    I tried that...I'm not good with editing code to that degree. Can anyone post a script that will only accomplish what I put in my first post?

    Thanks to all

    Comment


    • #3
      <script>
      if (document.cookie) {
      location.replace("returnvisit.htm");
      }else{
      location.replace("firstvisit.htm");
      document.cookie = "visited";
      }
      </script>

      Comment


      • #4
        Hi,

        Thanks again, but I'm still having trouble putting together exactly what I need out of the link you provided to make the cookie. I've got the coding put in the 2 pages already. Can you post the cookie making script that will make the cookie when the page loads (and only what is necessary)? There were too many errors when I tried it. And this will work in the popular browsers right?

        Looking forward to replies
        Last edited by cunning-fox; Jun 29, 2002, 12:02 AM.

        Comment


        • #5
          Sorry I'm tired. What about this:On index:

          <script>
          if (!document.cookie) {
          document.cookie = "visited";
          }
          </script>

          On other pages:

          <script>
          if (document.cookie) {
          location.replace("page1.htm");
          }else{
          location.replace("page2.htm");
          }
          </script>

          Comment


          • #6
            Thanks Clark, but that long code exactly the way you had it, did not record a cookie on my computer. See, one page should record a cookie named "visited" on the visitor's computer (when page loads), then if they go to page 1 and they don't have it, they go to page 2.

            Plus the functions don't work if users don't have the cookie, so it just gives errors (regarding the code on the 2 pages)

            I just need a simple script using javascript (IE and NS compatible), that will record a cookie named "visited" that will last 2 years, when the page loads. Then I'll use this check that XgooseX made to check the cookie from another page:

            <script>
            if (document.cookie) {
            location.replace("returnvisit.htm");
            }else{
            location.replace("firstvisit.htm");
            document.cookie = "visited";
            }
            </script>
            Last edited by cunning-fox; Jun 29, 2002, 03:35 PM.

            Comment


            • #7
              Can anyone else with knowledge of cookies make what I mentioned in my last post?

              Thanks

              Comment


              • #8
                Anyone?

                Comment


                • #9
                  Nevermind, I found a way.

                  For learning purposes, I'll post it. If you want to place a cookie on someone's computer with the page loading (no clicking buttons or submitting), then use the top part. Then check it on any other page and tell it where to go depending on whether they have the cookie or not (part 2 of code, compliments of x_goose_x)

                  Part 1

                  <head>

                  <SCRIPT LANGUAGE="JavaScript">
                  <!-- Begin
                  var expDays = 30;
                  var exp = new Date();
                  exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

                  function getCookieVal (offset) {
                  var endstr = document.cookie.indexOf (";", offset);
                  if (endstr == -1)
                  endstr = document.cookie.length;
                  return unescape(document.cookie.substring(offset, endstr));
                  }
                  function GetCookie (name) {
                  var arg = name + "=";
                  var alen = arg.length;
                  var clen = document.cookie.length;
                  var i = 0;
                  while (i < clen) {
                  var j = i + alen;
                  if (document.cookie.substring(i, j) == arg)
                  return getCookieVal (j);
                  i = document.cookie.indexOf(" ", i) + 1;
                  if (i == 0) break;
                  }
                  return null;
                  }
                  function SetCookie (name, value) {
                  var argv = SetCookie.arguments;
                  var argc = SetCookie.arguments.length;
                  var expires = (argc > 2) ? argv[2] : null;
                  var path = (argc > 3) ? argv[3] : null;
                  var domain = (argc > 4) ? argv[4] : null;
                  var secure = (argc > 5) ? argv[5] : false;
                  document.cookie = name + "=" + escape (value) +
                  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
                  ((path == null) ? "" : ("; path=" + path)) +
                  ((domain == null) ? "" : ("; domain=" + domain)) +
                  ((secure == true) ? "; secure" : "");
                  }
                  function DeleteCookie (name) {
                  var exp = new Date();
                  exp.setTime (exp.getTime() - 1);
                  var cval = GetCookie (name);
                  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
                  }

                  var favorite = GetCookie('animal');

                  if (favorite != null) {
                  switch (favorite) {
                  case 'cat' : url = 'login.html'; // change this to same page this code is on!
                  }
                  window.location.href = url;
                  }
                  // End -->
                  </script>
                  </HEAD>

                  <BODY onload="SetCookie('animal', this.name, exp);">
                  </body>




                  Part2

                  <script>
                  if (document.cookie) {
                  location.replace("returnvisit.htm");
                  }else{
                  location.replace("firstvisit.htm");
                  document.cookie = "cat";
                  }
                  </script>

                  Comment

                  Working...
                  X