Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-12-2005, 03:39 AM   PM User | #1
Lawn Gnome
New Coder

 
Join Date: Jul 2005
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Lawn Gnome is an unknown quantity at this point
Javascript Cookie Help

Ok here's my code

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?!?!?!
Lawn Gnome is offline   Reply With Quote
Old 08-12-2005, 10:28 AM   PM User | #2
iota
Registered User

 
Join Date: Oct 2004
Posts: 592
Thanks: 0
Thanked 1 Time in 1 Post
iota is an unknown quantity at this point
yeah, what do you wanna do with that ?
iota is offline   Reply With Quote
Old 08-12-2005, 12:47 PM   PM User | #3
Johnny Lang
New Coder

 
Join Date: Jul 2005
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Johnny Lang is an unknown quantity at this point
Code deleted. The constant harrassment here isn't worth my time.
__________________
www.javascript-demos.com

Last edited by Johnny Lang; 08-12-2005 at 09:42 PM..
Johnny Lang is offline   Reply With Quote
Old 08-12-2005, 11:58 PM   PM User | #4
Lawn Gnome
New Coder

 
Join Date: Jul 2005
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Lawn Gnome is an unknown quantity at this point
ok....how to i make them do stuff?????? im not that good in java =(
Lawn Gnome is offline   Reply With Quote
Old 08-13-2005, 01:48 AM   PM User | #5
Lawn Gnome
New Coder

 
Join Date: Jul 2005
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Lawn Gnome is an unknown quantity at this point
Quote:
Originally Posted by Johnny Lang
Code deleted. The constant harrassment here isn't worth my time.

WHAT?????!!!!! harrassment?!?!?!
Lawn Gnome is offline   Reply With Quote
Old 08-13-2005, 06:35 AM   PM User | #6
iota
Registered User

 
Join Date: Oct 2004
Posts: 592
Thanks: 0
Thanked 1 Time in 1 Post
iota is an unknown quantity at this point
Lawn Gnome, now I understand what you want with your code :

Then here's what you get :


Code:
<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>
iota is offline   Reply With Quote
Old 08-14-2005, 12:24 AM   PM User | #7
Lawn Gnome
New Coder

 
Join Date: Jul 2005
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Lawn Gnome is an unknown quantity at this point
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
Lawn Gnome is offline   Reply With Quote
Old 08-14-2005, 03:53 PM   PM User | #8
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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;

}
__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.
Mr J is offline   Reply With Quote
Old 08-16-2005, 03:24 AM   PM User | #9
Lawn Gnome
New Coder

 
Join Date: Jul 2005
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Lawn Gnome is an unknown quantity at this point
k thanks it works great
Lawn Gnome is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:20 PM.


Advertisement
Log in to turn off these ads.