...

View Full Version : Cookies Cookies and well, cookies!



DR.Wong
01-31-2005, 02:07 PM
Hi guys, does anyone know how to make a page set a cookie (with a value; yes, no or mby) automatically and then redirect the page to another once it has done so?

Can fix your Chopstick!
*Dr.Wong! :thumbsup:

Mr J
01-31-2005, 06:27 PM
Yes :D

DR.Wong
01-31-2005, 06:55 PM
Me say cool! You wanna let the secret out?

(please, I kinda need it!) ;)

*Wong

Mr J
01-31-2005, 07:40 PM
Well you did only ask if anyone knew how to do it


<script language=javascript>
<!--
days=365 // number of cookie days
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 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)
do_this()
}
else{}

}

function do_this(){
window.location="yourpage.htm"
}

chk_cookie()

// -->
</script>

DR.Wong
02-01-2005, 01:12 PM
Well I thought it was a little impolite to just say, HEY YOU! TELL ME HOW TO DO THIS!

Anyways, thanks for the script. :thumbsup: :)

Mr J
02-01-2005, 01:56 PM
I have to admit to being a bit puzzled with your request. :confused:

You link to a page that sets a cookie then immediately get redirected to another page. :(

Why not link straight to the other page and have the cookie set in that page. :)

DR.Wong
02-01-2005, 04:08 PM
You see the options of the page that the cookie page rediects to is determined by the cookie that is set on the cokie page, clicking a different link on the 'index' page will take you to a different cookie page.

Ultimately I would like a button that could set a cookie and be a link to another page!

But I have asked before and got no reply.

If you know how to do this, please let me know :thumbsup:

Can Fix your Chopstick!
*DR.Wong

DR.Wong
02-01-2005, 05:03 PM
This is one of the pages that I use to set cookies;
Now what I want is when you click on the setcookie button, it must set the cookie and go to lets say,'page2.htm' .

The Setcookie button can be found at the bottom of the code.

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var expDays = 365;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (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 getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.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 DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var favorite = GetCookie('examplecookie');

if (favorite != null) {
switch (favorite) {
case 'yes' : url = 'page1.htm'; // change these!
break;
case 'no' : url = 'page2.htm';
break;
case 'prompt' : url = 'page3.htm';
break;


}
window.location.href = url;
}
// End -->
</script>
</head>
<body>

<center><font face=verdana size=5 color=black>My Cookie Page</font><br>
<hr>
<p align=left><font face=verdana size=5 color=Black>Example Cookies</font>
<form>
<p align=left><table><tr><td>
<input type=button name="prompt" value=" prompt " onClick="SetCookie('examplecookie', this.name, exp);"><br><br>

<input type=button name="yes" value=" yes " onClick="SetCookie('examplecookie', this.name, exp);"><br><br>
<input type=button name="no" value=" no " onClick="SetCookie('examplecookie', this.name, exp);">




</td></tr>
</table>
</form>
</p>
</body>
</html>


I have also put in the cookie checker code (located above buttons) just incase you need it for some reason.

Can Fix Your Chopstick!
*DR.Wong



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum