I’m trying to set a cookie which every 30 days (or any other set time period) will launch for example Stackoverflow.com.
I found an answer here which i thought might help.
and here is my test code so far:
The HTML + Initiator
<html>
<head>
<!-- Load JQuery 1.8.3 and the repeatablePopup functions-->
<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/repeatablePopup.js"></script>
<script>
jQuery(document).ready(function() {
var visited = readCookie('visited');
if (!visited || visited !== "true") {
createCookie('visited', "true", 7);
} else {
window.open("http://stackoverflow.com");
}
});
</script>
</head>
<body>
<p>Setting Cookie</p>
</body>
</html>
The Funcitons on repeatablePopup.js
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
document.write('Cookie is set');
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Edit: problem now is that cookie is planted every time I refresh the page, even though the cookie exists.
So eventually the pop up isn’t working.
Thanks for any help given,
Shahar
Try this:
When the cookie is not exist, then the pop-up window will opened. After the pop-up window opens, you set the cookie. Thirty days later, the cookie will expired and everything will back to normal and do the same thing again.