make popup only appear once!

  • wiggywag
  • Newbie
  • Newbie
  • wiggywag
  • Posts: 13

Post 3+ Months Ago

Hi!

I've made a popup which loads automatically when people visit my index page. When people close this popup (made with javascript which opens a seperate html file in a popup window) I don't want this popup to pop up another time again to the same visitor!

If you have some code suggestion it would be fine!

Thanks!

- wiggywag
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I don't know how good an idea it is to use an unrequested popup, and I suspect a lot of people are blocking it anyway, but you could use a cookie to keep track of whether or not they've seen the page already. I found these cookie functions so long ago that I don't remember where to credit them, but I've used them successfully a couple of times:

Code: [ Select ]
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function setCookie(name, value, expires, path, domain, secure) {
 var curCookie = name + "=" + escape(value) +
   ((expires) ? "; expires=" + expires.toGMTString() : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "");
 document.cookie = curCookie;
}

function getCookie(name) {
 var dc = document.cookie;
 var prefix = name + "=";
 var begin = dc.indexOf("; " + prefix);
 if (begin == -1) {
  begin = dc.indexOf(prefix);
  if (begin != 0) return null;
 } else
  begin += 2;
 var end = document.cookie.indexOf(";", begin);
 if (end == -1)
  end = dc.length;
 return unescape(dc.substring(begin + prefix.length, end));
}

function pop()
{
  theUrl = "http://www.yahoo.com";
  window.open(theUrl,"theWindow","width=400,height=400,menubar=yes,toolbar=yes")
}

var seen = getCookie("seen");

if (!seen) {
var now = new Date();
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
setCookie("seen", 1, now);
pop();
}

//-->
</script>
</head>
<body>
blah
</body>
</html>
  1. <html>
  2. <head>
  3. <title>Untitled</title>
  4. <script type="text/javascript">
  5. <!--
  6. function setCookie(name, value, expires, path, domain, secure) {
  7.  var curCookie = name + "=" + escape(value) +
  8.    ((expires) ? "; expires=" + expires.toGMTString() : "") +
  9.    ((path) ? "; path=" + path : "") +
  10.    ((domain) ? "; domain=" + domain : "") +
  11.    ((secure) ? "; secure" : "");
  12.  document.cookie = curCookie;
  13. }
  14. function getCookie(name) {
  15.  var dc = document.cookie;
  16.  var prefix = name + "=";
  17.  var begin = dc.indexOf("; " + prefix);
  18.  if (begin == -1) {
  19.   begin = dc.indexOf(prefix);
  20.   if (begin != 0) return null;
  21.  } else
  22.   begin += 2;
  23.  var end = document.cookie.indexOf(";", begin);
  24.  if (end == -1)
  25.   end = dc.length;
  26.  return unescape(dc.substring(begin + prefix.length, end));
  27. }
  28. function pop()
  29. {
  30.   theUrl = "http://www.yahoo.com";
  31.   window.open(theUrl,"theWindow","width=400,height=400,menubar=yes,toolbar=yes")
  32. }
  33. var seen = getCookie("seen");
  34. if (!seen) {
  35. var now = new Date();
  36. now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
  37. setCookie("seen", 1, now);
  38. pop();
  39. }
  40. //-->
  41. </script>
  42. </head>
  43. <body>
  44. blah
  45. </body>
  46. </html>


That cookie is set to expire in one year (365 * 24 * 60 * 60 * 1000), but you can change the expiration date to whatever you want. For example, you could change the 365 to 1 and it would expire in one day. Keep in mind that some people set their browsers so that cookies expire after 90 days or after the session ends and some folks block cookies entirely, so there's no absolute guarantee that it will work for everyone. If cookies are blocked it won't work at all of course, if they are set to expire after the session ends then the person will see the popup the next time they visit, or after 90 days or however long they've set the cookies to expire.
  • wiggywag
  • Newbie
  • Newbie
  • wiggywag
  • Posts: 13

Post 3+ Months Ago

thanks a lot

this really helps!

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 123 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.