Solved

Need to change cookie to expire on session close

Posted on 2004-09-20
2
310 Views
Last Modified: 2010-05-18
Here is a cookie I am using that works great, but sets the expiration of the cookie to 1 year.  I need it to expire after each session.  In other words, when the user closes the browser I need the cookie to expire.  The script works great otherwise so I do not want to change anything EXCEPT the expiration to expire after each session.  If possible, please provide complete solution to script as I am not a Javascript programmer.  I found this script on a different EE posting and just changed the cookie name and value.

------------Set Cookie---------------------

<SCRIPT LANGUAGE="JavaScript">
<!--
dtNextYear = new Date();
dtNextYear.setTime(dtNextYear.getTime() + (365 * 24 * 60 * 60 * 1000)); // 365 days from now
setCookie('backupguard_login','BUGLoginYES',dtNextYear);
function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" +expire.toGMTString()))
}
 //-->
</SCRIPT


-----------Check Cookie---------------------

<SCRIPT LANGUAGE="JavaScript">
      <!--
      strMyCookie = getCookie('backupguard_login');

      function getCookie(name) {
      var search = name + "="
      if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search)
      if (offset != -1) { // if cookie exists
      offset += search.length
      end = document.cookie.indexOf(";", offset)
      if (end == -1)
      end = document.cookie.length
      return unescape(document.cookie.substring(offset, end))
     }
     }
     }
      if (getCookie('backupguard_login') != 'BUGLoginYES') {
      document.location = 'http://www.backupguard.com/site/client_login.cfm';//if they haven't logged in
      }
      //-->
      </SCRIPT>
     
<script language="javascript" type="text/javascript">
0
Comment
Question by:murryc
2 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
ID: 12103540
As far as I know you just (using the functions posted by you)

use setCookie('backupguard_login','BUGLoginYES') instead of setCookie('backupguard_login','BUGLoginYES', dtNextYear);

This will cause the cookie to have no explicit expire data, which will be interpreted by browsers as 'Expire on windowsclosed';

At least, this is how it usually works with cookies when they have no expire date set.
0
 

Author Comment

by:murryc
ID: 12105639
Works Great..thanks
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
email validation 9 47
href return to normal window 5 28
testing a web application for ipad on windows pc 3 34
JQuery conflicts/syntax issue 4 23
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

685 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