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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookies to check no. times window pops up

Status
Not open for further replies.

silverswim

Programmer
Oct 17, 2001
50
GB
hi,
I am trying to get the pop-up window to only come up once every browser session. I'm using cookies, but something is not working. Can anyone see where I am messing it up?
I put this in the head:

function setCookie()
{
var the_date = new Date("January 20, 2099");
var cookie_date = the_date.toGMTString();

var the_cookie = cookie_date;
the_cookie = the_cookie + ";expires= :" ;
document.cookie = the_cookie;
}

function readCookie()
{
var checkDate= the_date.toGMTString();
if !(the_cookie= checkDate){
setCookie();
MM_openBrWindow('popUpWindow.htm','FreeTrial','resizable=no,width=250,height=250');

}
}

then this went in the body section:

onLoad=" readCookie(); MM_preloadImages('currNaviOver/currentNaviBar-Over_05.jpg')"

Thanks v. much
silverswim
 
What you want is :

Code:
<script>
  function setCookie() {
    document.cookie = &quot;=all_ready_popup&quot;
  }

  function readCookie() {
    if (!(document.cookie == &quot;all_ready_popup&quot;)) {
      setCookie();
      MM_openBrWindow('popUpWindow.htm','FreeTrial','resizable=no,width=250,height=250');
    }
  }
</script>
<body onload=&quot;readCookie();MM_preloadImages('currNaviOver/currentNaviBar-Over_05.jpg')&quot;&quot;>
Regards

Big Dave

davidbyng@hotmail.com
 
I think neither one of those is quite correct. The proper format for a cookie is: &quot;name=value;expires=date&quot;. Your problem is that you've got the date in the wrong place.
Code:
function setCookie()
{
    var the_date = new Date(&quot;January 20, 2099&quot;);
    var cookie_date = the_date.toGMTString();
    
    var the_cookie = &quot;poppedup=yes;expires=&quot;+cookie_date;
    document.cookie = the_cookie;
}
That will set the cookie properly. To check the cookie, use this:
Code:
if (document.cookie.indexOf(&quot;poppedup=yes&quot;) >= 0) {
   // the window has already been popped up
} else {
   // the window has not been popped up
}
Since you want to do this once per session, you might not want to set the cookie to never expire though, which is what you're doing now. Just use a per-session cookie with no expiration date at all. Either that, or make sure that you delete the cookie before the person leaves your page. This function will delete your cookie:
Code:
function deleteCookie()
{
    var the_date = new Date();
    the_date.setFullYear(the_date.getFullYear()-1);
    var cookie_date = the_date.toGMTString();
    var the_cookie = &quot;poppedup=;expires=&quot;+cookie_date;
    document.cookie = the_cookie;
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
The script I made was correct! It is for a per-session cookie as requested not a date related cookie. Regards

Big Dave

davidbyng@hotmail.com
 
Dave, you're right, it IS a per-session cookie. BUT, the value you set and the value you look for aren't the same (the leading = in the set). Also, your code won't work if there are any other cookies set for the page, since document.cookie contains ALL the valid cookies for a page. That's why I used indexOf to search for the cookie value rather than ==. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
By the way, Dave, I sincerely apologize if my posting offended you in any way. I didn't intend to be insulting. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
No I wasn't offened, though your right if there were any other cookies then indexOf is the best method Regards

Big Dave

davidbyng@hotmail.com
 
Thankyou both. I have been away for a couple of days but will be trying these now.
Silverswim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top