...

View Full Version : Cookie Question



dlg0351
01-28-2005, 09:59 PM
Can someone help me with a cookie? I have a flash intro page that I only want to be displayed the first time that the page is viewed, other wise I want the visitor to be redirected to a new page or the page that would come after the flash intro.

I have seen other code on here that does something similar, such as the code below:

<html>
<head>
<title></title>

<SCRIPT LANGUAGE="JavaScript">
<!--
cookie_name = "Cookie1";

function doCookie()
{
gettimes();
if(document.cookie)
{
index = document.cookie.indexOf(cookie_name);
}
else
{
index = -1;
}
var expires = "Monday, 04-Apr-2010 05:00:00 GMT"
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)
{
redirect();
return null;
}
else
{
return null;
}
}
}
}

function redirect()
{
window.location="index2.html";
}
//-->
</SCRIPT>
</head>

<body onLoad="doCookie()">

This is the body of the page.

</body>
</html>


But this code still displays the flash page and then quickly redirects you to the second page. I do not want any of the original or flash page to be displayed at all, I just want you to be forwarded.

I was thinking that this would work but it does not display what I want in the body of the page:

<html>
<head>
<title></title>

<SCRIPT LANGUAGE="JavaScript">
<!--
cookie_name = "Cookie1";

function doCookie()
{
gettimes();
if(document.cookie)
{
index = document.cookie.indexOf(cookie_name);
}
else
{
index = -1;
}
var expires = "Monday, 04-Apr-2010 05:00:00 GMT"
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)
{
redirect();
return null;
}
else
{
showPage();
return null;
}
}
}
}

function redirect()
{
window.location="index2.html";
}

function showPage()
{
document.write("This is the body of the page.");
}
//-->
</SCRIPT>
</head>

<body onLoad="doCookie()">


</body>
</html>


Any help is greatly appreciated!

Thanks.
-Dave

Willy Duitt
01-29-2005, 06:21 PM
With the script you are using the script is not fired until the document is loaded... Either remove the doCookie function wrapper and accompying function call from the onload event or remove the onload event and add doCookie() immediately prior to the closing script tag...

.....Willy

dlg0351
01-31-2005, 04:07 AM
Exactly what I needed, thanks!

:thumbsup:

Willy Duitt
01-31-2005, 10:33 AM
Exactly what I needed, thanks!

:thumbsup:

Cheers... I am pleased to hear you understood... :thumbsup:
But, please tell me... Was it Either or Or...

.....Willy

dlg0351
01-31-2005, 02:38 PM
Both actually, I removed the onLoad event and the call to the function and it works perfectly. Here is the code in the header:

<SCRIPT language=JavaScript>
<!--
cookie_name="cookie_name"
expDays = 1;

var count = get_cookie('cookie_name');
if (count == null) {
count=1;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
set_cookie('Andreottis', count, exp);

whatever_function()
}

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();
}

Thanks again! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum