Hi cclegg06-ga,
I have posted the required code below. Here is an explanation of
how it works. The example HTML file contains checks for the cookie in
two places:
- once when the page is loaded, in the <body> tag's onLoad event.
If the cookie already exists, the page does not load the form. Instead
it shows a message, asking the user to wait for 90 days.
- The cookie is also looked for when the form's Submit button is
pressed. If the cookie is not present, a new cookie, set to expire in
90 days, is written, and the form is allowed to be sent. If the cookie
already exists, then the user is shown an error message, and the form
is not sent.
The Javascript file consists of four functions:
- CheckForCookie(): This function is the one that is put in the
<body> tag's onload event. It displays a message on the page if the
cookie already exists.
- CheckForm(): This function is put in the form's onSubmit event.
It allows the form to be submitted if the cookie does not
exist.Otherwise, the user sees an error message, and the form is not
submitted.
- WriteCookie(): This is the function that actually writes the
cookie. It takes in five arguments, for more information on these is
provided below.
- ExistsCookie(): This function returns true is the specified
cookie exists, else it returns false.
The WriteCookie function takes the following 5 arguments:
- name : name of the cookie.
- data : the data to be stored in the cookie.
- expiryDate : Thisis a Javascript Date object specifying the
expiry date for this cookie. Setting no expiration date on a cookie
causes it to expire when the browser closes. If you set an expiration
date in the past, the cookie is deleted.
- domain : Setting the domain of the cookie allows pages on a
domain made up of more than one server to share cookie information.
- path : Setting a path for the cookie allows the current document
to share cookie information with other pages within the same
domainthat is, if the path is set to /thispathname, all pages in
/thispathname and all pages in subfolders of /thispathname can access
the same cookie information.
Only the name and data values are necessary, you can set the rest
to null if you want.
Whenever you want a page to write a cookie, have it call the
WriteCookie function with suitable values for name, data and
expiryDate. If you want a page to display a message when the cookie is
found, have it call the CheckForCookie() function.
You can get the relevant source files from:
[HTML] : www31.brinkster.com/tanm/GA/cookie.htm
[Javascript]: www31.brinkster.com/tanm/cookie.js
Please Note: In the example HTML page, when you click on the Submit
button, the browser will submit the form, and then refresh the page.
When the browser refreshes the page, the CheckForCookie() function in
the <BODY> tag, detects the cookie and displays the 90 days message
instead of the form.
For your convenience, I have posted the code below:
+++++++++++++++++ Begin HTML+++++++++++++++++++
<html>
<head>
<script language="javascript" src="cookie.js">
</script>
</head>
<body onload="CheckForCookie();">
<center>
<form >
<input name="submit" type="submit" onclick="CheckForm();"
value="Submit">
</form>
</center>
</body>
</html>
++++++++++++++++++ End HTML++++++++++++++++++++
+++++++++++++++ Begin Cookie.js +++++++++++++++
var cname = "FormSubmitted" // name of the cookie
var data ="1"; // data to be stored(should not be null)
var cpath = ""; // path for which allowed [Optional]
var cdomain = "";// domain for which allowed [optional]
// Checks whether the cookie exists or not
// If the cookie exists, it display the specified message on the page
function CheckForCookie()
{
if( ExistsCookie(cname) )
{
document.write("<center><h1>You have already submitted the
form!!</h1>"
+"<br> <h3> You cannot submit this form again, until
three months "
+" (since the day you first submitted it) have elapsed.<br></h3>"
+"<h4> Thank You for your interest.</h4>"
+"</center>");
}
}
// Checks whether the form should be submitted or not
// If the cookie exists, the form is not submitted
// if no cookie exists, a new cookie, set to expire after
// three months ,is written and the form is submitted
function CheckForm()
{
if( !ExistsCookie(cname) )
{
// Write a cookie set to expire after 90 days
now= new Date(); // get current date and time
expiry = new Date();
expiry.setTime((now.getTime() + 90*24*60*60*1000)); // set to 90
days from now
WriteCookie(cname,data,expiry,cpath,cdomain); // Write cookie
return true;
}
alert("You have already submitted this form");
return false;
}
//Writes the specified data to the cookie file
function WriteCookie(name,data,expiryDate,path,domain)
{
if( name==null || name=="" || data==null || data=="")
{
alert("You must provide both name and data values to write the
cookie.");
return false;
}
var Cookie = name + "=" + escape(data)
+ ( (expiryDate)? "; expires=" +
expiryDate.toGMTString() : "" );
+ ((path)? "; path=" + path : "" )
+ ((domain)?"; domain=" + domain : "" );
Cookie +=";";
//Set cookie
document.cookie = Cookie;
return true;
}
//Checks if the specified cookie exists or not
function ExistsCookie(name)
{
// cookies are separated by semicolons
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
// a name/value pair is separated by an equal sign
var aCrumb = aCookie[i].split("=");
if (name == aCrumb[0])
return true;
}
// a cookie with the requested name does not exist
return false;
}
+++++++++++++++ End Coookie.js ++++++++++++++++
Be careful if you intend to copy and paste the above code. Due to the
way in which Google Answers formats all postings, very long lines of
code will get pushed onto the next line, resulting in syntax errors
when you try to run the code. I recommend that you download the
relevant files using the links provided above.
Hope this helps.
If you need any clarifications, or modifications to the code, just
ask!
Regards,
Theta-ga
:-) |
Clarification of Answer by
theta-ga
on
02 Apr 2003 13:38 PST
Hi cclegg06-ga,
I have reformatted the cookie.js file, so (hopefully) GA should no
longer have any problems with it. Try copying and pasting the code
below.
+++++++++++++++ Begin Cookie.js +++++++++++++++
var cname = "FormSubmitted" // name of the cookie
var data ="1"; // data to be stored(should not be null)
var cpath = ""; // path for which allowed [Optional]
var cdomain = ""; // domain for which allowed [Optional]
// Checks whether the cookie exists or not
// If it exists, it display a specified message on the page
function CheckForCookie()
{
if( ExistsCookie(cname) ) // If the cookie is found
{
// Show message
document.write("<html><head><title>"
+"The Form has already been submitted!!</title>"
+"</head><body><center>"
+"<h1>You have already submitted the form!!</h1><br><h3>"
+"You cannot submit this form again, until three months "
+"(since the day you first submitted it) have elapsed."
+"</h3><br><h4> Thank You for your interest.</h4>"
+"</center></body></html>");
}
}
// Checks whether the form should be submitted or not
// If the cookie exists, the form is not submitted
// if no cookie exists, a new cookie, set to expire after
// three months ,is written and the form is submitted
function CheckForm()
{
if( !ExistsCookie(cname) )
{
// Write a cookie set to expire after 90 days
now= new Date(); // get current date and time
expiry = new Date();
// Set expiry Date to 90 days from now
expiry.setTime((now.getTime() + 90*24*60*60*1000));
WriteCookie(cname,data,expiry,cpath,cdomain);
return true;
}
// The user has already clicked submit once
alert("You have already submitted this form");
return false;
}
// Writes the specified data to the cookie file
function WriteCookie(name,data,expiryDate,path,domain)
{
// The name and data arguments are compulsory
if( name==null || name=="" || data==null || data=="")
{
alert("You must provide both name & data values.");
return false;
}
var Cookie = name + "=" + escape(data)
+((expiryDate)?"; expires="+expiryDate.toGMTString():"")
+ ((path)? "; path=" + path : "" )
+ ((domain)?"; domain=" + domain : "" );
//Set cookie
document.cookie = Cookie;
return true;
}
//Checks if the specified cookie exists or not
function ExistsCookie(name)
{
// cookies are separated by semicolons
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
// a name/value pair is separated by an = sign
var aCrumb = aCookie[i].split("=");
if (name == aCrumb[0])
return true;
}
// a cookie with the requested name does not exist
return false;
}
+++++++++++++++ End Coookie.js ++++++++++++++++
Hope this helps.
Regards,
Theta-ga
:-)
|
Clarification of Answer by
theta-ga
on
05 Apr 2003 23:56 PST
Hi cclegg06-ga,
Here is an implementation of the unified javascript file which
brings together the functionality asked for in your previous three
questions. A couple of functions have been dropped, to be replaced by
the new SubmitForm() function. Minor changes have been made to some of
the other functions.
Here is a list of fuctions contained in the file:
- SubmitForm(): This function is called when the user clicks the
Submit button on the form. If the form has been filled in correctly,
and the user has not submitted this form before, it writes acookie to
the disk and allows the form to be submitted.
- VerifyForm(): This is called by the SubmitForm() fn to verify the
the form has been filled in correctly. Currently this function is
empty. You should replace it with code for checking and verifying the
form contents.
- Startimer(), StopTimer(), ResetTimer(): The same as defined in my
answer to one of your previous questions.
- CheckForCookie(),WriteCookie(), ExistsCookie(): The same as defined
in my answer above.
Here is a guide to calling these functions on your form page:
[ HTML Tag ] [ Event ] [ Function Name]
<BODY> OnLoad CheckForCookie()
<INPUT type="SUBMIT" OnClick SubmitForm()
<INPUT type="RESET" OnClick StopTimer()
Other Form Elements OnFocus StartTimer()
Please note: In some places, the code refers to certain form
elements(the hidden field, submit button) by name. Since the name you
use for your form elements will vary from what I use in my example,
you will have to change these lines before you can use this javascript
with your forms. The lines that have to be changed have been marked
with the following comment "//CHANGE".
You can get the sample HTML file and the Javascript code from:
[Html] ( http://www31.brinkster.com/tanm/GA/subOnce.htm )
[JS] ( http://www31.brinkster.com/tanm/GA/subOnce.js )
For your convenience, I have included the contents of the javascript
file (all 160 lines!!) below.
================== BEGIN subOnce.htm ======================
<html>
<head>
<script language="javascript" src="subOnce.js"></script>
</head>
<body onLoad="CheckForCookie();">
<FORM NAME="myForm" >
<PRE>
Button <INPUT TYPE="BUTTON" VALUE="Button"
onfocus="StartTimer();" >
CheckBox <INPUT TYPE="CheckBox" onfocus="StartTimer();">
File <INPUT TYPE="FILE" onfocus="StartTimer();" >
Hidden <INPUT TYPE="HIDDEN" name="myhiddenfield"
VALUE="0" >
Password <INPUT TYPE="PASSWORD" NAME="myPass
onfocus="FormClicked();" onchange="StartTimer();" >
Radio <INPUT TYPE="RADIO" NAME="myRadio" value="Radio"
onfocus="StartTimer();" >
Text <INPUT TYPE="TEXT" NAME="myText"
onfocus="StartTimer();" >
Textarea <TEXTAREA NAME="myTextarea"
onfocus="StartTimer();" ></TEXTAREA>
Select object <SELECT NAME="mySelect" onfocus="StartTimer();"
><OPTION>1<OPTION>2<OPTION>3</SELECT>
Reset object <INPUT TYPE="RESET" onfocus="ResetTimer();" >
Submit object <INPUT TYPE="SUBMIT" name="mysubmitbutton"
onclick="SubmitForm();" >
</PRE>
</FORM>
</body>
</html>
================== END subOnce.htm ========================
================== BEGIN subOnce.js =======================
var formSubmitted = false;
var timerStarted=false;
var startTime=0, stopTime=0, timeTaken=0;
var cname = "FormSubmitted" // name of the cookie
var data ="1"; // data to be stored(should not be null)
var cpath = ""; // path for which allowed [Optional]
var cdomain = ""; // domain for which allowed [Optional]
function SubmitForm()
{
//check if the form has been submitted before
if( formSubmitted || ExistsCookie(cname) )
{
alert("You have already submitted this form!");
formSubmitted = true;
document.myForm.mysubmitbutton.disabled=true; //CHANGE
return false;
}
// Form has not been submitted before
//Check if it was filled completely
if( verifyForm() == false )
{
alert ("Please complete the form before you submit it.");
return false; // Don't submit
}
// The form has been filled in completely. Submit it
StopTimer(); // Set the time value in the hidden field
formSubmitted = true; // Set the formSubmitted flag
// Write a cookie set to expire after 90 days
now= new Date(); // get current date and time
expiry = new Date();
// Set expiry Date to 90 days from now
expiry.setTime((now.getTime() + 90*24*60*60*1000));
WriteCookie(cname,data,expiry,cpath,cdomain);
// Show message to the user
alert("Submitting Form....\nThank you!");
return true;
}
function verifyForm()
{
// Use this function to ensure that all the
// required data has been entered in the form
// return true if the form is complete
// return false if its not
return true;
}
function StartTimer()
{
if( !timerStarted)
{
startTime = new Date();
startTime=startTime.getTime();// get time in milliseconds
timerStarted = true; //Timer Enabled
}
}
function StopTimer()
{
if( startTime ==0 || formSubmitted )
{
// Change myForm to your Form name
// Change myhiddenfield to your hiddenfield name
window.document.myForm.myhiddenfield.value=0; //CHANGE
}
else
{
stopTime = new Date();
stopTime=stopTime.getTime();
timeTaken = (stopTime - startTime)/(1000);
// Change myform to Your Form name
// Change myhiddenfield to your hiddenfield name
document.myForm.myhiddenfield.value= timeTaken; //CHANGE
}
//Reset the Timer
ResetTimer();
}
function ResetTimer()
{
//Reset Timer Info
startTime=0;
timerStarted=false; // Disable Timer
}
// Checks whether the cookie exists or not
// If it exists, it display a specified message on the page
function CheckForCookie()
{
if( ExistsCookie(cname) || formSubmitted )
{
// Show message [ Change this message to suit your needs ]
document.write("<html><head><title>"
+"The Form has already been submitted!!</title>"
+"</head><body><center>"
+"<h1>You have already submitted the form!!</h1><br><h3>"
+"You cannot submit this form again, until three months "
+"(since the day you first submitted it) have elapsed."
+"</h3><br><h4> Thank You for your interest.</h4>"
+"</center></body></html>");
}
}
// Writes the specified data to the cookie file
function WriteCookie(name,data,expiryDate,path,domain)
{
// The name and data arguments are compulsory
if( name==null || name=="" || data==null || data=="")
{
alert("You must provide both name & data values.");
return false;
}
var Cookie = name + "=" + escape(data)
+((expiryDate)?"; expires="+expiryDate.toGMTString():"")
+ ((path)? "; path=" + path : "" )
+ ((domain)?"; domain=" + domain : "" );
//Set cookie
document.cookie = Cookie;
return true;
}
//Checks if the specified cookie exists or not
function ExistsCookie(name)
{
// cookies are separated by semicolons
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
// a name/value pair is separated by an = sign
var aCrumb = aCookie[i].split("=");
if (name == aCrumb[0])
return true;
}
// a cookie with the requested name does not exist
return false;
}
================== BEGIN subOnce.js =======================
Hope this helps.
If you need any clarifications, just ask!
Regards,
Theta-ga
:-)
|