...

View Full Version : Javascript Cookie Help



Lawn Gnome
08-12-2005, 03:39 AM
Ok here's my code


<html>

<script>
<!--
var caution = false

function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "Todd" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "")
if (!caution || (name + "=" + escape(value)).length <= 4000)
document.cookie = curCookie
else
if (confirm("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}

function getCookie(name) {
var prefix = name + "Todd"
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}


function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "Todd" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT"
}
}

function fixDate(date) {
var base = new Date(0)
var skew = base.getTime()
if (skew > 0)
date.setTime(date.getTime() - skew)
}

var now = new Date()
fixDate(now)
now.setTime(now.getTime() + 31 * 24 * 60 * 60 * 1000)
var name = getCookie("name")
if (!name)
name = prompt("Please enter your favorite color:", "")
setCookie("color", name, now)
document.write("<body bgcolor=+ color + >")
//-->
</script>



</html>

now i cant get it to work! any help?!?!?!

iota
08-12-2005, 10:28 AM
yeah, what do you wanna do with that ? :)

Johnny Lang
08-12-2005, 12:47 PM
Code deleted. The constant harrassment here isn't worth my time.

Lawn Gnome
08-12-2005, 11:58 PM
ok....how to i make them do stuff?????? im not that good in java =(

Lawn Gnome
08-13-2005, 01:48 AM
Code deleted. The constant harrassment here isn't worth my time.


WHAT?????!!!!! harrassment?!?!?!

iota
08-13-2005, 06:35 AM
Lawn Gnome, now I understand what you want with your code : :)

Then here's what you get : :thumbsup:





<SCRIPT LANGUAGE = "JavaScript">
<!-- Original: Mattias Sjoberg -->
/*
Notes added by icebergz(http://dear.to/icebergz)

This script works in all major browsers; IE, Mozilla and Opera.
Bg color is just an example, you can extend the script use such as visitor name,bookmark let , popup once and many more.

Nice one!

*/
<!-- Begin
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function color(){
var favColor = GetCookie('color');
if (favColor == null) {
favColor = prompt("What is your favorite background color?");
SetCookie('color', favColor, exp);
}
document.bgColor=favColor;
return favColor;
}
function set(){
favColor = prompt("What is your favorite background color?");
SetCookie ('color', favColor, exp);
}
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();
}
// End -->
</SCRIPT>

</HEAD>


<center><BR><BR><BR>
<SCRIPT>
document.write('your favorite background color is : ' + color());
</SCRIPT>
<br>
<a href="JavaScript:set()">Change background color</a>
</center>

Lawn Gnome
08-14-2005, 12:24 AM
thanks but u need to add a auto.refresh somewhere and i dont know where, becuse u have to refresh the page after u chang the color.
but awsome script :thumbsup:

Mr J
08-14-2005, 03:53 PM
Just add

document.bgColor=favColor;

to the end of function set

function set(){
favColor = prompt("What is your favorite background color?");
SetCookie ('color', favColor, exp);

document.bgColor=favColor;

}

Lawn Gnome
08-16-2005, 03:24 AM
k thanks it works great



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum