Jump to content

Cookies On A Local File System?


casper3912

Recommended Posts

new message: never mind, there was something wrong with the code, still having a problem, but its fine i'll figure it out. if not, i'll ask.original message: I have a number of projects that cookies could come in use for. So i decided to learn how to use them. I think everything in my code is correct. I read here or there something about problems with safari and localHost when it comes to cookies. I'm just opening files up in a browser(FF and IE)i don't have a server set up or anything. Could that be a problem(since it wants a domain?) I googled, but...yea, so i'm asking you. i can post my code if you want.

Link to comment
Share on other sites

I got it working :) it changes css href values. a user uses a drop down box(HTML select element) to select a style sheetwhat 'sheet is stored' is then stored in a cookie. any ways to see to improve it?Script

/* NOTICE:This notice shouldn't be removed.Author: Casper. To reader: feel free to use, redistribute, change,etc., the code just don't try to stop others from freely doing the same. Also, this code will be available at http://www.plainfield.k12.in.us/hschool/studentinterests/just follow the soon to be up(current date:may 15, 2009 ) 'web Design' link. Then in the code library somewhere will be this code with a more through explanation. *//* ABSTRACT-onLoadCSSCheck()change css based of of cookie values-----------------------------INDEXget cookie valueif there is a cookiemake the herf value of the css link into that value*/function onLoadCSSCheck() {HrefValue = readCookie('CSSHref');if (HrefValue!=null && HrefValue!="")		{	var Link = document.getElementById('MainCSS');	Link.setAttribute('href',HrefValue);}}window.onload = onLoadCSSCheck;/* change css based off of form imput.....................................................................ABSTRACT onCSSOptionChange()Changes a hard coded Link Element's herf value to a value stored in a hard coded select box--------------------------------------------------INDEXmake a pointer to the correct select BoxRetrieve the selected Option's valueget the link to be changedchange the herf value of the link to the value stored in the selected option's Value attribute. if need be, store a cookie*/function onCSSOptionChange() {var ThemeSelectBox = document.getElementById('Theme'); /* check what option is selected */var SelectedOption=ThemeSelectBox.selectedIndexvar SelectedValue =ThemeSelectBox.getElementsByTagName("option")[SelectedOption].value	var Link = document.getElementById('MainCSS');Link.setAttribute('href',SelectedValue);if(SelectedValue =='DefualtCSS.css')	{			HrefValue = readCookie('CSSHref');		if (HrefValue!=null && HrefValue!="")		{			eraseCookie('CSSHref');		}	}else{eraseCookie('CSSHref');createCookie('CSSHref',SelectedValue,30)}}/* very good cookie resource: http://www.quirksmode.org/js/cookies.htmlthe next three functions are from there, PPK rules. */function createCookie(name,value,days) {	if (days) {		var date = new Date();		date.setTime(date.getTime()+(days*24*60*60*1000));		var expires = "; expires="+date.toGMTString();	}	else var expires = "";	document.cookie = name+"="+value+expires+"; path=/";}function readCookie(name) {	var nameEQ = name + "=";	var ca = document.cookie.split(';');	for(var i=0;i < ca.length;i++) {		var c = ca[i];		while (c.charAt(0)==' ') c = c.substring(1,c.length);		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);	}	return null;}function eraseCookie(name) {	createCookie(name,"",-1);}

HTML:

<html><head><link id ="MainCSS" rel="stylesheet" type="text/css" href="DefualtCSS.css" /><script> Script above goes here. </script></head><body><form><select id="Theme" onChange="onCSSOptionChange()">  <option value="DefualtCSS.css">Defualt Theme</option>  <option value="CoolCSS.css">Cool Theme</option>  <option value="WarmCSS.css">Warm Theme</option>  <option value="DarkCSS.css">Dark Theme</option></select></form><div><ul><li>hello </li><li>nana no no ooo yeaaa. </li></ul><p> Testing testing testing</p></div></body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...