Forum Moderators: open

Message Too Old, No Replies

Help with submit button and cookies

         

nort99

2:44 pm on Aug 22, 2010 (gmt 0)

10+ Year Member



Please help me out. This script is not mine, it works fine for me, except when you close the browser it make the submit button active again. So If I change var DaysToLive = "1"; It works but does not disable the cookie at midnight or change of date.

What I would like to do is, disable the submit button until midnight or change of day every day. The redirect page works great. Please help me out, I am not very good with scripts as you can tell. Thanks.

<script type="text/javascript" language="JavaScript">
<!--

// Three items can be customized
// (values between quotation marks):
//
// 1.
// What URL shall the browser be redirected
// to if a cookie was previously set?

var RedirectURL = "sample.htm";


// 2.
// How many days shall the cookie live in
// the visitor's browser (0 means remove
// cookie whenever browser closes)?

var DaysToLive = "0";


// 3.
// What name shall the cookie have (any
// sequence of alphabetical characters
// is okay, so long as the name doesn't
// conflict with any other cookies that
// this web page might be setting.)?

var CookieName = "HasVisited";


// // // // // // // // // // // // // // // //
// // // // // // // // // // // // // // // //
// // // //
// // No other customizations required. // //
// // // //
// // // // // // // // // // // // // // // //
// // // // // // // // // // // // // // // //

function Action() {
location.href = RedirectURL;
}

DaysToLive = parseInt(DaysToLive);
var Value = 'bypass page next time';

function GetCookie() {
var cookiecontent = '';
if(document.cookie.length > 0) {
var cookiename = CookieName + '=';
var cookiebegin = document.cookie.indexOf(cookiename);
var cookieend = 0;
if(cookiebegin > -1) {
cookiebegin += cookiename.length;
cookieend = document.cookie.indexOf(";",cookiebegin);
if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
cookiecontent = document.cookie.substring(cookiebegin,cookieend);
}
}
if(cookiecontent.length > 0) { return true; }
return false;
}

function SetCookie() {
var exp = '';
if(DaysToLive > 0) {
var now = new Date();
then = now.getTime() + (DaysToLive * 23 * 59 * 59 * 999);
now.setTime(then);
exp = '; expires=' + now.toGMTString();
}
document.cookie = CookieName + '=' + Value + exp;
return true;
}

if(GetCookie() == true) { Action(); }
// -->
</script>

Fotiman

1:33 pm on Aug 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The code for calculating "then" looks a little strange. Also, DaysToLive should be a number, not a string. Take a look at this example to see how you might modify your code to correctly calculate the "then" date.


// The current date
var now = new Date();
var DaysToLive = 1;
// Start by setting the date to the current one
var then = new Date(now.getTime());
// Use setDate to add days
then.setDate(then.getDate() + DaysToLive);
// Create a new date using only the year, month, and date
// This will set the time to 00:00:00 (midnight)
then = new Date(then.getFullYear(), then.getMonth(), then.getDate());
alert(now + "\n" + then );