Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set cookie/get cookie problem

Status
Not open for further replies.

cyan01

Programmer
Joined
Mar 13, 2002
Messages
143
Location
US
Hi, Experts!

I wrote a small piece of js code. It is supposed to do the following:

On exit, it detects a cookie "nopop" first. If found the cookie, do nothing. If not, popup a new window and set the "nopop" cookie.

My problem seems random.

1) If I see the "nopop" cookie in my

C:\Documents and Settings\...\Cookies

subdir, then I can not get the cookie by calling getCookie().

2) If I do NOT see the "nopop" cookie in my

C:\Documents and Settings\...\Cookies

subdir, then I CAN get the cookie by calling getCookie().

How strange it is!!

The following is my code:

Code:
<HTML>
<HEAD>
<TITLE>cookie test</TITLE>
  <script language=JavaScript>
  function closeit()
  {
    window.open('javascript:window.close()','newwin');
  }

  function openit(sURL, name)
  {
    var name1 = "nopop";
    var value = "0123456789";
    var expireDate = new Date();
    expireDate.setTime(expireDate.getTime() + (24*60*60*1000));
    if(!getCookie(name1) )
    {
      newwindow=window.open(sURL,"newwin","width=300, height=300");
      setCookie(name1,value,expireDate,"/",null,true);
    }
  }

  function setCookie(name1,value,expires,path,domain,secure)
  {
   document.cookie = name1 + "=" +escape(value) +
   ((expires) ? ";expires=" + expires.toGMTString() : "") +
   ((path) ? ";path=" + path : "") +
   ((domain) ? ";domain=" + domain : "") +
   ((secure) ? ";secure" : "");
  }

  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;
  }
  </script>
</HEAD>
<body bgcolor="#ffffff" onUnload="javascript:openit('/pops/invite.html', 'id')" onload="javascript:closeit()">
<script language="javascript">
var cookieVal = getCookie("nopop");
document.write ("<h3>cyan cookieVal = #" + cookieVal + "#</h3>");
</script>
</body>
</html>

You may need make your own invite.html for your test.

Thank you so much for you help.
 
More to follow.

The code above could be a little misleading. When I call openit, I passed in another variable "id", but it is not used, 'cause it will be used later. I should have cleaned it up. But I forgot. Sorry for the confusion.

Many thanks for your attention and help.
 
Can someone please help me on this problem?

Many thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top