SEO | Link Popularity | Search Engine Consulting | SEO Tutorial | SEO Tools | SEO Forum
Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-10-2005, 03:40 PM
CarolinaN CarolinaN is offline
SEO Junior
 
Join Date: Jun 2005
Posts: 2 CarolinaN is on a distinguished road
Setting BG Color with a cookie function

I appreciate everyone that has looked at my posts. I have one last script to understand from the book that I am studying, and then I am off to the book store to get a better book. So if you will please look at this code and let me know what I am doing wrong I will be very grateful.

I simply want to learn how to create a cookie that is assigned the user’s favorite color. And change the background color of the document to that color. So far I think I may be storing it in the cookie correctly, and I have a function that sets the background color, but I don’t think I am recalling it properly when I reopen the web page.


Code:
<html>
<head>
<title>Set BGcolor with cookie</title>
<script language="JavaScript">
var yourcolor=""
function makeCookie(form){
	var when = new Date();
	when.setTime(when.getTime() + 24 * 60 * 60 * 1000);
					// 24 hours from now
	when.setFullYear(when.getFullYear() + 1);
					// One year from now
	ycolor=document.form1.yourcolor.value;
	document.cookie=escape("BGcolor")+"="+escape(ycolor)+
		";expires="+when.toGMTString();
	alert(document.cookie);
		document.bgColor=document.form1.yourcolor.value;

}



	function setColor(form) {
		ycolor=document.form1.yourcolor.value;
		var position=document.cookie.indexOf("BGcolor=");
			if(document.cookie == ""){
			alert("No cookies  totay");
			return false;
			}
		else {
			if ( position != -1){
			var begin = position + 5;
			var end=document.cookie.indexOf(";", begin);
			if(end == -1)
				{ end=document.cookie.length;
				}
			ycolor= unescape(document.cookie.substring(begin, end));
		document.bgColor=document.form1.yourcolor.value;
		}}
	}
</script>
</head>
<body onLoad="setColor()">
<h2>Background Color</h2>

<form name="form1">
<p>What color would you like the background to be? <br>
	<input type="text" name="yourcolor" size="30">
	</p>
	<p><input type="button" value="Make cookie" onclick="makeCookie();">
	</p>

</form></body></html>

CarolinaN
Reply With Quote
  #2  
Old 06-11-2005, 12:36 AM
Paz's Avatar
Paz Paz is offline
SEO GUY Moderator
 
Join Date: Sep 2004
Location: Antalya, Turkey
Posts: 4,113 Paz has a spectacular aura aboutPaz has a spectacular aura about
Hi Carolina,

firstly welcome to the forum!

I'm sorry no one's answered your post.

I abandoned all but the most basic javascript because of security issues (and service pack 2), cross-browser issues, and it's not se friendly much of the time. I prefer to use plain html or asp now, but many use php. Tell us a bit more about the aims of your site, and we can make some general suggestions about how you should move forward - maybe save you some time!

I think you have a duff book! There are a few really good web resources with online tutorials or sites where you can copy and paste working solutions that you can modify.

Try this one first (for creating cookies):
http://www.js-examples.com/javascript/?view=665

Then this more advanced one for modifying cookies (but has documentation):
http://www.quirksmode.org/js/cookies.html

Also, sometimes the most simple code works ok in IE6, but doesn't in IE 5, Netscape/Mozilla (and vice versa) without some modification, so you should test your scripts with IE6, IE5, NS7, and maybe NS 4.79.

Cheers,
Paz.
__________________
10.3 million entries for Hotels in Turkey but I'm still chipping away.

Last edited by Paz : 06-11-2005 at 12:46 AM.
Reply With Quote
Reply


Thread Tools
Display Modes 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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Login/Register
User Name
Password
Remember Me?

Forum Links
Forum Home
SEO Forum
Internet Marketing Forum
Web Design Forum
Web Hosting Forum
Programming Forum
SEO Chat

Quick Links
Forum Home
New Posts
Mark Forums Read
Open Buddy List
User Control Panel
Edit Avatar
Edit Profile
Edit Options
Miscellaneous
Subscribed Threads
My Profile

Search Forums

Advanced Search
All times are GMT -8. The time now is 08:33 AM.


Powered by: vBulletin Version 3.0.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.