views:

77

answers:

2

I think I will need to use javascript to do this so I put it here on stack overflow. So, I have two themes for my website, the only difference is that one is a solid colour (background), the other a repeated image, so I didn't make separate css files for them. I have two links in the navigation bar that change it (with javascript). In the css file its the solid colour, so when ever the page loads it starts out as that. When the image theme link is clicked, it sets the document.body.backgroundImage to the image, and when the solid colour theme link is pressed it just sets the background image to "" (empty), so that you can see the colour again. So how can I make the theme persistent, not changing when ever the user goes to another page, as well as when they return another time. Thanks.

EDIT: I can use either PHP or javascript.

A: 

You can use a session to keep track of the user's preferences on the serverside. When the user comes back on and has a theme selected (which you can determine from the session variables) then the server will deliver HTML that has backgroundImage set instead of a solid color. When the user toggles preferences you can send out an ajax request that updates the session variables.

Kai
Could you explain a bit more how to actually do that?
Mk12
It depends a lot on your server side coding. Are you using a CMS or framework like Joomla or Drupal, a blogging platform or some kind, and does it use PHP or the like? I've added a JS example if you don't have any control or experience modifying the server side code but I agree, doing it server side is a better solution (you can combine the two also, set the cookie with JS as I describe and then read it with PHP and print out the right theme in the first place)
Alex JL
I can use php, but other then that right now its just plain html and javascript.
Mk12
Ok, I did what you suggested, setting cookies with javascript, loading appropriate css theme with php. Thanks!
Mk12
+1  A: 

If you're using all javascript and don't have any serverside code to work with, here's a JS example to set and read a cookie:

Add this function to your JS, then run it when the theme is changed:

function set_theme(name){
  document.cookie='sel_theme='+name+';';
  }//so, run set_theme('whatevername'); when it is set by the user

To read the cookie and set the theme on page load (using jQuery or similar $(document).ready() would be better than onload, though, but here's a straight js/dom example)

 window.onload=function(){
var cookie_pos=document.cookie.indexOf('sel_theme=');//locate value in cookie
if(cookie_pos!=-1){//if string was found
  var cookie_flavor=substr(cookie_pos+10,document.cookie.indexOf(';',cookie_pos));//extract the value of the cookie
  /*then run your already existing change theme function using the extracted name*/
  }
Alex JL
Thanks I'll try this tomorrow.
Mk12
Great, i'll also note you can do javascript:alert(document.cookie) in the address bar or type document.cookie into the console in firebug to see current cookie contents.
Alex JL
I could also use PHP. Would that be better than javascript? If so, how would I do it in PHP?
Mk12
Set the cookie with JS when it's changed by the user, and then when the page is next loaded, read the cookie with PHP's $_COOKIE superglobal and print the appropriate CSS/HTML for that theme. I'll add a server side answer example later if it helps, but a lot depends on how your CSS/HTML is set up.
Alex JL
Thanks I figured it out.
Mk12