Anthony2oo4 01-26-2005, 01:37 PM Hey guys, this is probbily a really simple script but I have searched the site and google but cant seen to find the answers im looking for.
I have a flash intro on my site, and i want to have some javascript on the page so that it will set a cookie and next time the intro is accessed, it redirects to the index.php file. But i want the cookie to expire in a week.
Can someone put me straight on where im going wrong as i have no clue what to do :(
Thanks for your time,
Anthony
EDIT:
I managed to find this script
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=/";
}
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;
}
but I dont think its finished. How do i tell it to redirect to index.php if the cookie is found etc. If anyone is willing, could they please finish this script?
Please try the following
<SCRIPT language=JavaScript>
<!--
cookie_name="poponce"
expDays = 7; // number of days the cookie should last
function get_cookie (name) {
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 get_cookie_val (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function get_cookie_val(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function set_cookie (name, value,expires) {
var argv = set_cookie.arguments;
var argc = set_cookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function delete_cookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = get_cookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
function checkCount() {
var count = get_cookie('poponce');
if (count == null) {
count=1;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
set_cookie('poponce', count, exp);
dothis()
}
// add onload="checkCount()" to the opening BODY tag
}
function dothis(){
window.location="index.php"
}
// -->
</SCRIPT>
Insert the onLoad event handler into your BODY tag.
<BODY onload="checkCount()">
Anthony2oo4 01-26-2005, 08:50 PM Hey,
Thanks for yout help, I tried that code, but it seemed to have the opisite approach. The first time i visited the page, it went to google. Then from there after, it stayed at the page.
I uploaded it here.
www.whereisit.org/hello.htm
dlg0351 01-26-2005, 09:24 PM Try this code:
<html>
<head>
<title></title>
<SCRIPT LANGUAGE="JavaScript">
<!--
cookie_name = ""; //enter Cookie name
function doCookie() {
gettimes();
if(document.cookie)
{index = document.cookie.indexOf(cookie_name);}
else
{index = -1;}
var expires = "Wednesday, 02-Feb-2005 05:00:00 GMT" //set your expiration date
if (index == -1)
{
document.cookie=cookie_name+"=1; expires=" + expires;
}
else
{
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1)
{
countend = document.cookie.length;
}
count = eval(document.cookie.substring(countbegin, countend)) + 1;
document.cookie=cookie_name+"="+count+"; expires=" + expires;
}
}
function gettimes()
{
if(document.cookie)
{
index = document.cookie.indexOf(cookie_name);
if (index != -1)
{
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1)
{
countend = document.cookie.length;
}
count = document.cookie.substring(countbegin, countend);
if (count >= 1) //change the 1 to the # of times before you are forwarded to index.php
{
redirect();
return ("");
}
else
{
return ("");
}
}
}
return ("");
}
function redirect()
{
location.href="index.php";
}
//-->
</SCRIPT>
</head>
<body onLoad="doCookie()">
Flash Intro
</body>
</html>
I did this today after I saw your question because I realized that I could use this script on a webpage I made.
My applogies, looks like I posted the wrong script, please try the following
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script language=javascript>
<!--
days=7
cookie_name="redirect"
function get_cookie (name) {
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 get_cookie_val (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function get_cookie_val(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function set_cookie (name, value,expires) {
var argv = set_cookie.arguments;
var argc = set_cookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function delete_cookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = get_cookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
function chk_cookie() {
var count = get_cookie(cookie_name);
if (count == null) {
count=1
var exp = new Date();
exp.setTime(exp.getTime() + (days*24*60*60*1000));
set_cookie(cookie_name,count, exp);
}
else{
window.location="http://www.google.com"
}
}
// -->
</script>
</HEAD>
<BODY onload="chk_cookie()">
Index
</BODY>
</HTML>
Anthony2oo4 01-27-2005, 03:22 PM Thank you both of you. Worked Great :D
|
|