Posted By

valcartei on 10/04/07


Tagged

javascript popup cookie


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

robotoverlord
gedittest
vali29
heinz1959


Pop up once


 / Published in: JavaScript
 

URL: http://javascript.internet.com/cookies/only-popup-once.html

displays a popup window only the first time the visitor gets to the website for expDays. It uses cookie to do that. expDays set the n. of days you want the cookie to live, default set to 1.

  1. in PopupOnce.js
  2. -----------------------------------
  3. var expDays = 1; // number of days the cookie should last
  4.  
  5. var page = "only-popup-once.html";
  6. var windowprops = "width=300,height=200,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes";
  7.  
  8. function GetCookie (name) {
  9. var arg = name + "=";
  10. var alen = arg.length;
  11. var clen = document.cookie.length;
  12. var i = 0;
  13. while (i < clen) {
  14. var j = i + alen;
  15. if (document.cookie.substring(i, j) == arg)
  16. return getCookieVal (j);
  17. i = document.cookie.indexOf(" ", i) + 1;
  18. if (i == 0) break;
  19. }
  20. return null;
  21. }
  22.  
  23. function SetCookie (name, value) {
  24. var argv = SetCookie.arguments;
  25. var argc = SetCookie.arguments.length;
  26. var expires = (argc > 2) ? argv[2] : null;
  27. var path = (argc > 3) ? argv[3] : null;
  28. var domain = (argc > 4) ? argv[4] : null;
  29. var secure = (argc > 5) ? argv[5] : false;
  30. document.cookie = name + "=" + escape (value) +
  31. ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  32. ((path == null) ? "" : ("; path=" + path)) +
  33. ((domain == null) ? "" : ("; domain=" + domain)) +
  34. ((secure == true) ? "; secure" : "");
  35. }
  36.  
  37. function DeleteCookie (name) {
  38. var exp = new Date();
  39. exp.setTime (exp.getTime() - 1);
  40. var cval = GetCookie (name);
  41. document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
  42. }
  43.  
  44. var exp = new Date();
  45. exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
  46.  
  47. function amt(){
  48. var count = GetCookie('count')
  49. if(count == null) {
  50. SetCookie('count','1')
  51. return 1
  52. } else {
  53. var newcount = parseInt(count) + 1;
  54. DeleteCookie('count')
  55. SetCookie('count',newcount,exp)
  56. return count
  57. }
  58. }
  59.  
  60. function getCookieVal(offset) {
  61. var endstr = document.cookie.indexOf (";", offset);
  62. if (endstr == -1)
  63. endstr = document.cookie.length;
  64. return unescape(document.cookie.substring(offset, endstr));
  65. }
  66.  
  67. function checkCount() {
  68. var count = GetCookie('count');
  69. if (count == null) {
  70. count=1;
  71. SetCookie('count', count, exp);
  72. window.open(page, "", windowprops);
  73. } else {
  74. count++;
  75. SetCookie('count', count, exp);
  76. }
  77. }
  78.  
  79. window.onload=checkCount;
  80. --------------------------------------------
  81. HTML, in the head:
  82. <script type="text/javascript" src="onlyPopupOnce.js"></script>

Report this snippet  

You need to login to post a comment.