I am trying to write a code that will display a pop-up message when a site is first visited and I want it to never display again if I already see it.. Here's my code, I hope someone could help me to prevent the pop up from appearing when I refresh the page although it shouldn't.
<SCRIPT LANGUAGE="JavaScript">
<!--
function GetCookie(cookie) {
var arg=name+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen) {
var j=i+alen;
if (document.cookie.substring(i,j)==arg)
return "here";
i=document.cookie.indexOf(" ",i)+1;
if (i==0) break;
}
return null;
}
var visit=GetCookie("cookie");
if (visit==null){
alert("Your Message Goes here and you only get to see it once!");
var expire=new Date();
expire=new Date(expire.getTime()+7776000000);
document.cookie="cookie=here; expires="+expire;
}
// -->
</SCRIPT>
I am trying to write a code that will display a pop-up message when a site is first visited and I want it to never display again if I already see it.. Here's my code, I hope someone could help me to prevent the pop up from appearing when I refresh the page although it shouldn't.
<SCRIPT LANGUAGE="JavaScript">
<!--
function GetCookie(cookie) {
var arg=name+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen) {
var j=i+alen;
if (document.cookie.substring(i,j)==arg)
return "here";
i=document.cookie.indexOf(" ",i)+1;
if (i==0) break;
}
return null;
}
var visit=GetCookie("cookie");
if (visit==null){
alert("Your Message Goes here and you only get to see it once!");
var expire=new Date();
expire=new Date(expire.getTime()+7776000000);
document.cookie="cookie=here; expires="+expire;
}
// -->
</SCRIPT>
Share
Improve this question
edited Jun 17, 2013 at 6:14
icedwater
4,9063 gold badges38 silver badges53 bronze badges
asked Jun 17, 2013 at 6:01
MeganMegan
651 gold badge3 silver badges7 bronze badges
6
- What exactly is the problem? – Mark Parnell Commented Jun 17, 2013 at 6:02
- Save my poor addled brain: what's the problem you're experiencing? – user1864610 Commented Jun 17, 2013 at 6:03
- Sorry i forgot to add that.. when I refresh the page the popup always shows up that's my problem – Megan Commented Jun 17, 2013 at 6:07
- my fist instinct would be to check getCookie and define a proper setCookie. Try using the methods in this page at last example w3schools./js/js_cookies.asp – kishu27 Commented Jun 17, 2013 at 6:18
- Perhaps this answer can help: stackoverflow./questions/17128637/… – user1693593 Commented Jun 17, 2013 at 6:20
1 Answer
Reset to default 2You have problems in your getCookie function. Try with this:
CODE
function getCookie(c_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1) {
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1) {
c_value = null;
} else {
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1) {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start, c_end));
}
return c_value;
}
example here: http://jsfiddle/TWB68/1/