Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading alternative CSS files

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
Maybe bit of an odd question, but I have a site which is fairly bland in terms of colour and I want to create either 4 or 5 CSS files which contain different colours or perhaps just a colour switch??

Basically, the site has two colours - white and dark colour which is used for the text and the background to some divs.
It also has two logos on it and I will recreate these with the other colours.
I'm looking to create maybe 4 or 5 colours (red, blue, etc) which will load at random when the page loads. My site is scripted in PHP.

I suppose I would want the colour scheme to remain constant whilst the user is on the site, so every new visitor will get a random colour (from my 4 or 5 colours) and then remain on that colour whilst they visit different pages on that site for their visit.

Does that all makes sense?
My thoughts were around just loading a different colour variable for the text and div backgrounds, but I suspect I'll need 4 or 5 different CSS files.
 
Well, the randomness of it is that whenever someone visits the website, a random 'style' (out of say a list of 4 or 5) is selected.

I did some digging around and found this solution which I think it similar to what I want - Coding Forums

The second part of my issue though is that I don't want a random colour/style (based on the above solution) to be loaded on every page load. I want the colour/style that is 'selected' upon the visitors first visit to be constant throughout their visit (obviously it will reset once they leave the site and then come back).
Maybe done with a cookie or in a SESSION variable?
 
Hi

d0nny said:
Maybe done with a cookie or in a SESSION variable?
Unless you already use sessions for something else, I would not involve them. ( Certainly not with the default settings, where using sessions adds [tt]Cache-control[/tt] HTTP response header to prohibit caching. )

A cookie would be enough to store the name of the randomly picked theme. That you can handle with JavaScript too. For example if you want to play nice you can link all the available stylesheets into your document, specifying in the [tt]rel[/tt] attribute [tt]stylesheet[/tt] for the chosen one and [tt]alternate stylesheet[/tt] for the others. Then let the visitors choose another one and apply it with JavaScript without reloading the page. ( Shameless advertisement : see the site in my signature for an example. )

Feherke.
 
Thanks Feherke.
Not sure I want to offer style changing as I'm hoping the colours will be subtle enough to simply give a slight change of scenery!
Plus, adding the opportunity to change up to 5 colours will, IMO, add further cluster to the pages.

I like the idea of picking the random stylesheet from my earlier link, its just I want the randomness to 'stick' once the user visits the site.
 
I agree with feherke. Simply set up a cookie with an expiration. If the cookie is present and valid you can read it to see what scheme was loaded. If not randomly select a scheme, and load the stylesheet using PHP. Then write out the cookie, with the selected theme, so you can use it again.


Code:
<head>
<?PHP
$styles=('style.css','style2.css','style3.css'...)

if(cookie_exists and isValid){
read_coookie();
$style=$styles[cookiecontents];
}
else{
random()
$style=$styles[random];
create_cooke($random);
}

echo '<LINK REL=StyleSheet HREF="' . $style . '" TYPE="text/css" MEDIA=screen>';
?>

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks for the advice and suggestions.
I've managed to implement the randomness of picking a stylesheet from 4 (I initially had problems with the '.' and '..' within the directory listing and 'array_slice' didn't seem to filter these out!) but I haven't implemented the stickiness yet.

I'm not sure I even want it now as it works well - there are only 4 or pages so a change of scenery is not too diverse.

Thanks again...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top