SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    css style changer with javascript - using cookies

    hi, I'm having trouble with the style changer on my site, on my PC it works in Chrome, Firefox and IE but not Safari and on another PC it doesn't work in chrome. here it is

    http://pixel-void.com/web_standards/index.html

    use the links at the very bottom to change the style.

    here is the javascript code for setting and getting the cookies for changing the style

    Code:
    function style_choice(style){
    	if(style == undefined){
    		style = 'main';
    	}
    	var txt = '<link rel="stylesheet" type="text/css" href="styles/' + style + '.css">';
    	return txt;
    }
    
    function setCookie(value){ //called from setCSS function
    	var the_date = new Date();
    	the_date.setTime(the_date.getTime()+(30*24*60*60*1000));
    	var final_date = the_date.toGMTString();
    	document.cookie = 'style_cookie=' + value + '; expires=' + final_date + '; path=/';
    }
    
    function getCookie(){
    	var my_cookie = document.cookie.split(';');
    	my_cookie = "x" + my_cookie //add any char to my cookie to make it a string
    	return my_cookie.split("=")[1]; //get the value after =
    }
    
    function setCSS(style){ // call when user clicks style link
    	setCookie(style); //set the new cookie
    	window.location.reload(); //reload the page
    }
    
    var the_cookie = getCookie(); // get cookie info
    var default_style = style_choice(the_cookie); // set style with cookie info

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The getCookie function is flawed because it relies on there being only one cookie stored on the domain.
    It needs to be recoded to look for the named cookie and return its value if any.
    Tab-indentation is a crime against humanity.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Proper Cookie handling functions are to be found at http://snipplr.com/view/45954/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by adamscott View Post
    JavaScript is the client side scripting language then what is the basic difference between the JAVA & JavaScript??
    See A Brief History of Javascript
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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