Link to home
Start Free TrialLog in
Avatar of cbonnock
cbonnock

asked on

Create cookie on page open... delete on page close.

I have a pop-up window. What I would like is for a javascript code to create a cookie when the page opens and delete it when the page is closed. It will be used to add/remove users from a chat list. (php chat) I do not know much about cookies, I would appreciate if you could throw in how to retrieve the info from the cookie... thanks.

Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

put this in a file called cookie.js

// cookie.js file
if (!window.daysToKeep) daysToKeep = 14; // default cookie life...
today      = new Date();
expiryDate = new Date(today.getTime() + (daysToKeep * 86400000));


/* Cookie functions originally by Bill Dortsch */

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

function getCookie(Name) {
   var search = Name + "="
   if (document.cookie.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(document.cookie.substring(offset, end))
      }
   }
}
function delCookie(name,path,domain) {
   if (getCookie(name)) document.cookie = name + "=" +
      ((path)   ? ";path="   + path   : "") +
      ((domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}
Then I suggest you add this to the parent window:
<script src="cookie.js" type="text/javascript"language="JavaScript"></script>

and this in the popup:

<html>
<head>
<script type="text/javascript"language="JavaScript">
if (opener && !opener.closed) opener.setCookie('cookieName','cookieValue')
</script>
</head>
<body onUnload="if (opener && !opener.closed) opener.delCookie('cookieName')">

Michel

Avatar of cbonnock
cbonnock

ASKER

For the purposes of a chat users list, all i need is to keep a username in the cookie... do i need to define everything else?
Encryption!!!, if that's all someone is identified by, then you just get your self a cookie editor, and change your username and then your someone else.
The cookie script I gave you is a real save-and-forget script. It is also needed if your server sets session cookies or if you have other cookies than the userName

Here is a simpler alternative

in parent window:
<script>
userName="unknown";
</script>

<html>
<head>
<script type="text/javascript"language="JavaScript">
if (opener && !opener.closed) parent.userName='fred'
</script>
</head>
<body onUnload="if (opener && !opener.closed) parent.userName='unknown';">
in the popup you can then access parent.userName all the time

No need for cookie.


Alternative 3 if you insist on cookie :

In parent:

<script>
function setUser(user) {
  document.cookie='userName='+user
}
function getUser() {
  return document.cookie.substring(9); // return what is after "userName="
}
</script>

in popup:

<html>
<head>
<script type="text/javascript"language="JavaScript">
if (opener && !opener.closed) parent.setUser('fred')
</script>
</head>
<body onUnload="if (opener && !opener.closed) parent.setUser('unknown');">
in the popup you can then access parent.getUser() all the time
You you set the cookie to expire -1, so as soon as the page is shut it is then deleted?
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial