Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Retrieving the value of a cookie stored earlier in the script

    I have pieced together some code that i've tested and everything works great except one thing.

    The script takes and stores the name of the cookie, which in this case is "ColorTheme" and stores the given value of either blue, red, or yellow. Then reloads the page. The next part of the script fetches the value of the cookie stored and depending on that value, whether it be blue, red, or yellow, loads different style sheets.

    Now if i create a function that just retrieves the value of the cookie then alerts to the user what the value is, then have a button that is clicked on that calls for that function, it works fine. But when I call that function to run automatically at a certain point in the script, the value comes back undefined.

    Im going to leave brief comments on certain parts of my code.
    Code:
    <script type="text/javascript">
    <!--
    
    // Cookie function to create the actual cookie
    var Cookies = {
    	init: function () {
    		var allCookies = document.cookie.split('; ');
    		for (var i=0;i<allCookies.length;i++) {
    			var cookiePair = allCookies[i].split('=');
    			this[cookiePair[0]] = cookiePair[1];
    		}
    	},
    	create: function (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=/";
    		this[name] = value;
    	},
    	erase: function (name) {
    		this.create(name,'',-1);
    		this[name] = undefined;
    	}
    };
    Cookies.init();
    
    // function to create a cookie with the value blue
    function saveItb(name) {
    	var b = 'blue';
    	if (b) {
    		eraseIt();
    		Cookies.create(name,b,365);
    	location.reload(true);
    	}
    }
    
    // function to create a cookie with the value red
    function saveItr(name) {
    	var r = 'red';
    	if (r) {
    		eraseIt();
    		Cookies.create(name,r,365);
    		location.reload(true);
    	}
    }
    
    // function to create a cookie with the value yellow
    function saveIty(name) {
    	var y = 'yellow';
    	if (y) {
    		eraseIt(name);
    		Cookies.create(name,y,365);
    		location.reload(true);
    	}
    }
    
    // function to erase the old cookie
    function eraseIt(name) {
    	Cookies.erase(name);
    }
    
    //function to load the value of the cookie and depending on the value loads a series of style sheets
    function readIt(name) {
    	var color = Cookies[name]; // this is the part that is not working
    	alert(color); // this returns the above value as undefined
    // everything below this works
    	if (color=="blue") {
    		var browser = navigator.appName
    		if (browser=="Opera")
    	 	{
    			document.write('<link rel="StyleSheet" type="text/css" href="ddsmoothmenuop.css">');
    			document.write('<link rel="StyleSheet" type="text/css" href="operab.css">');
    	 	}
    		else if (browser=="Microsoft Internet Explorer")
    	 	{
    			document.write('<link rel="StyleSheet" type="text/css" href="ieb.css">');
    			document.write('<style type="text/css">\n'+
    			'html .ddsmoothmenu{height: 1%;}\n'+
    			'</style>');
    		document.write('<link rel="stylesheet" type="text/css" href="ddsmoothmenu.css" />');
    	 	}
    		else
    	 	{
    			document.write('<link rel="StyleSheet" type="text/css" href="styleblue.css">');
    			document.write('<link rel="stylesheet" type="text/css" href="ddsmoothmenu.css" />');
    	 	}
    	}
    
    	else if (color=="red") {
    		var browser = navigator.appName
    		if (browser=="Opera")
    	 	{
    			document.write('<link rel="StyleSheet" type="text/css" href="ddsmoothmenuop.css">');
    			document.write('<link rel="StyleSheet" type="text/css" href="operar.css">');
    		 }
    		else if (browser=="Microsoft Internet Explorer")
    		 {
    			document.write('<link rel="StyleSheet" type="text/css" href="ier.css">');
    			document.write('<style type="text/css">\n'+
    			'html .ddsmoothmenu{height: 1%;}\n'+
    			'</style>');
    			document.write('<link rel="stylesheet" type="text/css" href="ddsmoothmenu.css" />');
    		 }
    		else
    		 {
    			document.write('<link rel="StyleSheet" type="text/css" href="stylered.css">');
    			document.write('<link rel="stylesheet" type="text/css" href="ddsmoothmenu.css" />');
    		 }
    	}
    
    	else if (color=="yellow") {
    		var browser = navigator.appName
    		if (browser=="Opera")
     		{
    			document.write('<link rel="StyleSheet" type="text/css" href="ddsmoothmenuop.css">');
    			document.write('<link rel="StyleSheet" type="text/css" href="operay.css">');
    		 }
    		else if (browser=="Microsoft Internet Explorer")
    		 {
    			document.write('<link rel="StyleSheet" type="text/css" href="iey.css">');
    			document.write('<style type="text/css">\n'+
    			'html .ddsmoothmenu{height: 1%;}\n'+
    			'</style>');
    			document.write('<link rel="stylesheet" type="text/css" href="ddsmoothmenu.css" />');
    		 }
    		else
    		 {
    			document.write('<link rel="StyleSheet" type="text/css" href="styleyellow.css">');
    			document.write('<link rel="stylesheet" type="text/css" href="ddsmoothmenu.css" />');
    		 }
    	}
    
    	else {
    		var browser = navigator.appName
    		if (browser=="Opera")
    	 	{
    			document.write('<link rel="StyleSheet" type="text/css" href="ddsmoothmenuop.css">');
    			document.write('<link rel="StyleSheet" type="text/css" href="operab.css">');
    	 	}
    		else if (browser=="Microsoft Internet Explorer")
    	 	{
    			document.write('<link rel="StyleSheet" type="text/css" href="ieb.css">');
    			document.write('<style type="text/css">\n'+
    			'html .ddsmoothmenu{height: 1%;}\n'+
    			'</style>');
    		document.write('<link rel="stylesheet" type="text/css" href="ddsmoothmenu.css" />');
     		}
    		else
    	 	{
    			document.write('<link rel="StyleSheet" type="text/css" href="styleblue.css">');
    			document.write('<link rel="stylesheet" type="text/css" href="ddsmoothmenu.css" />');
    	 	}
    	}
    }
    
    readIt(name);
    
    // -->
    </script>
    Last edited by kingswifty96; 05-28-2009 at 11:34 PM.

  • #2
    New Coder
    Join Date
    May 2009
    Posts
    58
    Thanks
    8
    Thanked 4 Times in 4 Posts
    First I think you need to put the variable name in all the eraseIt functions. Also why have a function leading to another function. so lets just make it Cookies.erase(name)
    Code:
    function saveItb(name) {
    	var b = 'blue';
    	if (b) {
    		Cookies.erase(name);
    		Cookies.create(name,b,365);
    	location.reload(true);
    	}
    }
    
    // function to create a cookie with the value red
    function saveItr(name) {
    	var r = 'red';
    	if (r) {
    		Cookies.erase(name);
    		Cookies.create(name,r,365);
    		location.reload(true);
    	}
    }
    
    // function to create a cookie with the value yellow
    function saveIty(name) {
    	var y = 'yellow';
    	if (y) {
    		Cookies.erase(name);
    		Cookies.create(name,y,365);
    		location.reload(true);
    	}
    }
    Next the way i got this script to work was to give your variables a name. Say you want to call the cookie "style" Well in the readIt('style'); function add the cookie name. Then when you call the functions to save the style. make sure you use the same cookie name.

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks for the tips, i realized that when i was telling the script to run the readIt function i wasnt putting in the actual name within the brackets. So it was trying to fetch a cookie with the name "name".

  • #4
    New Coder
    Join Date
    May 2009
    Posts
    58
    Thanks
    8
    Thanked 4 Times in 4 Posts
    You are very welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •