/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Nag Counter

Hello again,

Once more I am perplexed on an assignment. I cannot find help anywhere for this so any guidance would be most appreciated. I have a site where every THIRD time a person visits a site, they are reminded to register unless they have already registered. This site relies on cookies to store registration info. I have no idea how to get the “nag” counter to work with the cookie. Please help!
Here’s what I have so far:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>

<html xmlns=”http://www.w3.org/1999/xhtml“>

<head>
<title>Nag Counter</title>
<script type=”text/javascript”>

function registerForm()
{ var rName = document.register.name.value;
var rEmail = document.register.email.value;
var curDate = new Date();
var latDate= (curDate.getYear() + 1);
document.cookie = “fullname=rName;expires=latDate” +;
document.cookie = “fullmail=rEmail”;

window.alert(‘Please remember to register’)

window.alert(‘Thank you for registering’)

}
</script>

</head>
<body>
<h1>Registration</h1>
<h4>Please take a moment to register</h4>

<form action=”” name=”register”>
Name:
</br>
<input type=”text” name=”name” size=”60″>
</br>
E-mail Address:
</br>
<input type=”text” name=”email” size=”60″>
</br></br>
<input type=”button” name=”register” value=”Register” onclick=”registerForm();”>

</form>

<script type=”text/javascript” >

</script>

</body>
</html>

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@jssupernoobauthorNov 19.2010 — Still have found nothing that creates a nag counter. This is ridiculous.
Copy linkTweet thisAlerts:
@FangNov 19.2010 — The logic is to always read the cookie(s) onload.

If no cookie exist, set one. [I]count=1[/I]

If the user registers then delete this cookie or write user data to it.

When the user logs in again read the cookie, if there's no user data then [I]count=2[/I].

3rd time display a nag.
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 19.2010 — Okay, I'll try that out and see if it works.
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 19.2010 — Still can't get anything added to my code. I have zero guidance with how to work with cookies that count the number of times you visit so I'm really confused. Here's exactly what the textbook is asking for:

Create a document with a "nag" counter that reminds users to register. Save the counter in a cookie and display a message reminding users to register every THIRD time they visit your site. Create a form in the body of the document that includes text boxes for a users's name and e-mail address along with a Register button. Once a user fills in the text boxes and clicks the Register button, delete the nag counter cookie and replace it with cookies containing the user's name and e-mail address. After registering, display the name and e-mail address cookies in an alert message whenever the user revisits the site.
Copy linkTweet thisAlerts:
@FangNov 20.2010 — Read up on cookie use first: http://www.quirksmode.org/js/cookies.html
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 21.2010 — Now I just need the nag counter. It should remind the visitor to register every third time they visit. Anyone?
[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Nag Counter</title>
<script type="text/javascript">


function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}

}
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function loadForm()
{//NAG COUNTER GOES HERE

}

function registerForm()
{
username=getCookie('username');
usermail=getCookie('usermail');
if (username!=null && username!="")
{
window.alert('Thank you for registering!');
window.alert('Username:'+username+' E-mail:' +usermail);
}
else
{
username=document.register.name.value;
usermail=document.register.mail.value;
if (username!=null && username!="")
{
setCookie('username',username,365);
}
}
}


</script>

</head>
<body>
<h1>Registration</h1>
<h4>Please take a moment to register</h4>

<form action="" name="register" onload="loadForm();">
Name:
</br>
<input type="text" name="name" size="60">
</br>
E-mail Address:
</br>
<input type="text" name="mail" size="60">
</br></br>
<input type="button" name="registerbutton" value="Register" onclick="registerForm();">

</form>

<script type="text/javascript" >

</script>

</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@FangNov 21.2010 — The form onload will not work.[CODE]window.onload=function() {
// code here is check for existing registration
// else set/increment nag cookie
};
[/CODE]
Copy linkTweet thisAlerts:
@Logic_AliNov 21.2010 — Now I just need the nag counter. It should remind the visitor to register every third time they visit. Anyone?[/quote]
I renamed the function [FONT=Courier New]regNag[/FONT]. I just adapted it from existing code of my own. I had to fix your [FONT=Courier New]setCookie[/FONT] function to set session cookies.

It is set to alert the specified message on the first [I]load[/I] of every third [I]session[/I].

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;

&lt;head&gt;
&lt;title&gt;Nag Counter&lt;/title&gt;
&lt;script type="text/javascript"&gt;


function getCookie(c_name)
{
if (document.cookie.length&gt;0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}

}
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie = c_name+ "=" +escape(value) +

( expiredays ? (";expires="+exdate.toUTCString()) : "" );

}

function regNag( sessionCount, message, durationDays )
{
var cookieName = 'nagCount',
sessionName = cookieName + 'Session',
days = durationDays || 30,
prev = isNaN( prev = Number( getCookie( cookieName ) ) ) ? 0 : prev;

if( !getCookie( sessionName ) )
{
setCookie( sessionName, 'OK', 0 );

<i> </i>if( ++prev == sessionCount )
<i> </i>{
<i> </i> alert( message );
<i> </i> prev = 0;
<i> </i>}

<i> </i>setCookie( cookieName, prev, days );
}
}

function registerForm()
{
username=getCookie('username');
usermail=getCookie('usermail');
if (username!=null &amp;&amp; username!="")
{
window.alert('Thank you for registering!');
window.alert('Username:'+username+' E-mail:' +usermail);
}
else
{
username=document.register.name.value;
usermail=document.register.mail.value;
if (username!=null &amp;&amp; username!="")
{
setCookie('username',username,365);
}
}
}


window.onload = function(){ regNag( 3, 'Please Register', 300 ); }

&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Registration&lt;/h1&gt;
&lt;h4&gt;Please take a moment to register&lt;/h4&gt;

&lt;form action="" name="register" &gt;
Name:
&lt;/br&gt;
&lt;input type="text" name="name" size="60"&gt;
&lt;/br&gt;
E-mail Address:
&lt;/br&gt;
&lt;input type="text" name="mail" size="60"&gt;
&lt;/br&gt;&lt;/br&gt;
&lt;input type="button" name="registerbutton" value="Register" onclick="registerForm();"&gt;

&lt;/form&gt;


&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 22.2010 — Awesome! Thank you again, Logic Ali! You've been a big help with this cookie disaster.
×

Success!

Help @jssupernoob spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...