[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-05-13
7
Medium Priority
?
204 Views
Last Modified: 2008-02-01
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.

0
Comment
Question by:cbonnock
  • 4
  • 2
7 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16677125
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";
}
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16677129
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

0
 

Author Comment

by:cbonnock
ID: 16679506
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:UnexplainedWays
ID: 16679725
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.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16680224
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
0
 
LVL 12

Expert Comment

by:UnexplainedWays
ID: 16680237
You you set the cookie to expire -1, so as soon as the page is shut it is then deleted?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 16680386
In my first example, I call delCookie which sets the date to the past. That is the standard way.
If you want to kill it IMMEDIATELY, you can do

setCookie('userName',''); // clear it
delCookie('userName'); // delete it

In the other examples, I just set the cookie to "unknown" - "" is also ok.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question