Solved

How change multi theme with css file

Posted on 2006-10-23
6
306 Views
Last Modified: 2008-03-17
HI, I make a website which have function changing theme. On the header website I put 3 colours, when I click on the colour then my site is changed by this selected colour.
My site use css2, I have 3 css file. I want to use them to change theme
Note: when I close the browser or the site is refreshed then selected theme not change
To make reference: http://msn.com
Note: some computer can't create cookie so when I use javascript to create and  read cookie then my site is not run function theme
Please show me solution use PHP language
Thank you
0
Comment
Question by:VIETSKY
  • 2
6 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17793126
VIETSKY,

I'm not real sure if I understood the question correctly but if cookies aren't supported or cookies are disabled then PHP will not work either.  PHP, and even other server languages, would use cookies to do what you want.  If the reason it didn't work is JavaScript being disabled or not supported then but cookies do work then you could try PHP.

You set a PHP cookie using:

setcookie("cookiename", "value");  // needs to be set before HTML sent to browser

You can retrieve the cookie (after it is set and created) using:

$_COOKIE["cookiename"]

This should at least get you going.  Please let me know if I misunderstood the question.  Let me know if you have any questions or need more information.

b0lsc0tt
0
 

Author Comment

by:VIETSKY
ID: 17793167
I  had a solution as:
<link rel="stylesheet" type="text/css" href="css/style1.css" title="style1" />
<link rel="alternate stylesheet" type="text/css" href="css/style2.css" title="style2" />
<link rel="alternate stylesheet" type="text/css" href="css/style3.css" title="style3" />
<script type="text/javascript" src="js/styleswitcher.js"></script>

Content of styleswitcher.js below:
But when I use this solution then some computer can't create cookie so my site can't load style. Do you have other solution to me, thank you

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 250 total points
ID: 17793306
Without some server side way to keep track of this (i.e. login and database) the only option would be to require cookies to be enabled.  If it isn't that important then you can allow visitors to not support cookies but a different language will not get around this issue.

Do you visitors need to have this or is it just a convenience?  Are you willing to require cookies?  Unfortunately it can even be difficult to really test if cookies are disabled or not.  Also telling your users how to enable cookies may not be easy.

Let me know if you have any other questions related to this.
0
 
LVL 44

Assisted Solution

by:scrathcyboy
scrathcyboy earned 250 total points
ID: 17793594
You cannot really do what you are trying to do.  You want to change the CSS file based on a user click, but the CSS file is ALREADY loaded for the browser session.  Therefore you MUSt have a server refresh to load a different CSS file.  And this means running a server response, which is slow.

If all you are trying to do is change colors / styles on a user click, you simple do this with JS, not PHP, and it is very simple to change the style of a cell or table or sheet by JS using the onclick handler.  It is almost impossible NOT to run JS for websites any more, the 1% that do not allow it, just ignore them.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now