# Javascript Cookies



## dudeking (Feb 7, 2007)

This is a script to change the theme of my site. But unfortunately it dosnt work.....


```
<?php
			if(isset($_COOKIE["colorscheme"])){
				if($_COOKIE["colorscheme"] == "black"){
					echo '';
				}elseif($_COOKIE["colorscheme"] == "white"){
					echo '';
				}
			}else{
				echo '';
			}
			echo '';
		?>
```


```
<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>
```


```
[URL=#]
				[/URL]
```
Edit:

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


----------



## yankees26an (Mar 27, 2007)

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


```
if($_COOKIE["colorscheme"] == "black"){
                    echo '';
                }elseif($_COOKIE["colorscheme"] == "white"){
                    echo '';
                }
            }else{
                echo '';
```


----------



## MMJ (Oct 15, 2006)

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.


----------



## dudeking (Feb 7, 2007)

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?


----------



## MMJ (Oct 15, 2006)

http://www.faqts.com/knowledge_base/view.phtml/aid/2147/fid/128

Play with that. If you need help post back. 

EDIT:

I found a function that works with IE but not with FF:

document.styleSheets[0].href = "./styles/newstyle.css";


----------

