1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Javascript Cookies

Discussion in 'Web Design & Development' started by dudeking, Jul 12, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. dudeking

    dudeking Thread Starter

    Joined:
    Feb 7, 2007
    Messages:
    483
    This is a script to change the theme of my site. But unfortunately it dosnt work.....

    PHP:
    <?php
                
    if(isset($_COOKIE["colorscheme"])){
                    if(
    $_COOKIE["colorscheme"] == "black"){
                        echo 
    '<link href="/style/black.css" rel="stylesheet" type="text/css" />';
                    }elseif(
    $_COOKIE["colorscheme"] == "white"){
                        echo 
    '<link href="/style/black.css" rel="stylesheet" type="text/css" />';
                    }
                }else{
                    echo 
    '<link href="/style/black.css" rel="stylesheet" type="text/css" />';
                }
                echo 
    '<link href="/style/all.css" rel="stylesheet" type="text/css" />';
            
    ?>
    Code:
    <script type="text/javascript">
    			//Colour Scheme Cookie Set
    			function setcolorscheme(){
    				sitetheme = getCookie(colorscheme){
    					if(sitetheme == black){
    						function setCookie(colorscheme,white,expiredays){
    							var exdate=new Date()
    							exdate.setDate(exdate.getDate()+expiredays)
    							document.cookie=colorscheme+ "=" +escape(value)+
    							((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
    						}
    					}
    					if(sitetheme == white){
    						function setCookie(colorscheme,black,expiredays){
    							var exdate=new Date()
    							exdate.setDate(exdate.getDate()+expiredays)
    							document.cookie=colorscheme+ "=" +escape(value)+
    							((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
    						}
    					}
    				}
    			}
    		</script>
    HTML:
    <a onclick="setcolorscheme()" style="width:50px; height:50px; display:block;" href="#">
    				</a>

    Edit:

    http://dudeking.co.uk its set to the button in the top right which says white and has a down arrow
     
  2. yankees26an

    yankees26an

    Joined:
    Mar 27, 2007
    Messages:
    46
    Can you explain the problem a little bit more? What error do you get, if any?

    Is the white and black cookie supposed to link to the same stylesheet, because your code loads black.css on all instances

    PHP:
    if($_COOKIE["colorscheme"] == "black"){
                        echo 
    '<link href="/style/black.css" rel="stylesheet" type="text/css" />';
                    }elseif(
    $_COOKIE["colorscheme"] == "white"){
                        echo 
    '<link href="/style/black.css" rel="stylesheet" type="text/css" />';
                    }
                }else{
                    echo 
    '<link href="/style/black.css" rel="stylesheet" type="text/css" />';
     
  3. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    I could point out the errors in your code but the way you are doing things aren't that smart.

    How does the php code get executed? The setcolorscheme() function doesn't refresh the page.
     
  4. dudeking

    dudeking Thread Starter

    Joined:
    Feb 7, 2007
    Messages:
    483
    Well basicly nothing happens, no cookie is set and the style sheet doesnt change.

    Thank you for pointing out that both stylesheets are the same. I'll change that now!

    Well, I have never really done any Javascript before espicly nothing this advanced. I have just presumed much of the syntax to be the same as php which is wrong. Should I use all javascript or should I some how make that function refresh the page?
     
  5. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - Javascript Cookies
  1. Ravikumar112
    Replies:
    1
    Views:
    114
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/594817

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice