Ok, so I’ve done a style switcher in php, and I’m using it as a hidden feature on my site, which is triggered when user types “teema”, and a modal box will appear on the top of the page content and displaying the available themes.
<ul>
<li><a href="teema.php?teema=default" class="ajaxLink">Normaali</a></li>
<li><a href="teema.php?teema=superhero" class="ajaxLink">Superhero</a></li>
<li><a href="teema.php?teema=spacelab" class="ajaxLink">Spacelab</a></li>
<li><a href="teema.php?teema=united" class="ajaxLink">United</a></li>
<li><a href="teema.php?teema=cyborg" class="ajaxLink">Cyborg</a></li>
</ul>
However, I want to change the theme without the page refreshing so the css should load on background. Here’s the teema.php:
<?php
$style = $_GET['teema'];
setcookie("teema", $style, time()+604800);
if(isset($_GET['js'])) {
echo $style;
}
else{
header("Location: ".$_SERVER['HTTP_REFERER']);
}
?>
And the php on the index that checks the cookie:
<?php
if(!empty($_COOKIE['teema'])) $style = $_COOKIE['teema'];
else $style = 'default';
?>
<link rel="stylesheet" href="css/<? echo $style;?>.css" id="theme">
How could I load the new css on the background, and fade it over to the old css using jQuery?
You can simply make an ajax request so the cookie will be returned
Using jQuery:
This will parse the
teemavalue from linkhref, make call to thathref( may need to add path info) and updatehrefof link tag with ID=theme