@DownPW After spending a lot of time on the theme switcher (see referenced post above), I re-wrote some of it as found that there were caching issues with CSS as it always calls the same version number. The new function below resolves that issue.
I also removed the website prefix, as with relative paths, this is not required
// ------------------------------------------
// Theme Switcher
// ------------------------------------------
// On choisit une chaine de caractĆØre alĆ©atoire pour affecter un numĆ©ro de version au fichier CSS.
// The function below creates a random string which we use for CSS versioning to prevent cache conflicts
function generateRandomString(length) {
var text="";
var possible="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i=0;
i < length;
i++) {
text+=possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
$(document).ready(function () {
var string=generateRandomString(10);
$("#random_string").text(string);
// This variable gets the theme ID
// Cette variable obtient l'ID du thĆØme
var whichTheme=localStorage.getItem("theme");
// This variable gets the active theme's actual URL
//Cette variable obtient l'URL rĆ©elle du thĆØme actif
var activeTheme=localStorage.getItem("activeTheme");
// This variable appends the dropdown list to the existing panel
// Cette variable ajoute la liste dƩroulante au panneau existant
var panel=$('<li id="switcher" class="dropdown text-center"> \
<label for="theme-control-list-check" class="dropdown-toggle" data-toggle="dropdown" id="theme_dropdown" title="" role="button" data-original-title="Theme" aria-expanded="false"> \
<a class="btn-link" title="Theme Switcher" href="#"><i id="ticon" class="fa fa-fw fa-lightbulb-o"></i><span class="visible-xs-inline">Theme Switcher</span></a> \
</label> \
<ul id="theme" class="dropdown-menu"> \
<li><a id="default" href="#" rel="/assets/client.css?v=e02phpkima0">Default</a></li> \
<li><a id="default dark" href="#" rel="/assets/customcss/default_dark.css">Default Dark</a></li> \
<li><a id="Light Flat" href="#" rel="/assets/customcss/testflat.css">Light Flat</a></li> \
<li><a id="Dark Flat" href="#" rel="/assets/customcss/testflat.css">Dark Flat</a></li> \
<li><a id="Dark Neon Aqua" href="#" rel="/assets/customcss/dark_neon_aqua.css">Dark Neon Aqua</a></li> \
<li><a id="Dark Neon Blue" href="#" rel="assets/customcss/dark_neon_blue.css">Dark Neon Blue</a></li> \
<li><a id="Dark Neon Green" href="#" rel="/assets/customcss/dark_neon_green.css">Dark Neon Green</a></li> \
<li><a id="Dark Neon Solary" href="#" rel="/assets/customcss/dark_neon_solary.css">Dark Neon Solary</a></li> \
<li><a id="Dark Neon White" href="#" rel="/assets/customcss/dark_neon_white.css"">Dark Neon White</a></li> \
<li><a id="Eva 00" href="#" rel="/assets/customcss/eva_00.css">Eva.00</a></li> \
<li><a id="Eva 01" href="#" rel="/assets/customcss/eva_01.css">Eva.01</a></li> \
<li><a id="Eva 01 Purple Green"href="#" rel="/assets/customcss/eva_01_purple_green.css">Eva.01: Purple-Green</a></li> \
<li><a id="Eva 02" href="#" rel="/assets/customcss/eva_02.css">Eva.02: Red-Orange</a></li> \
</ul> \
</div> ');
// See if there is an active theme selected in localStorage. If none selected, use the default. If there is a theme in localStorage, use that and apply it
// Regarde s'il y a un thĆØme actif sĆ©lectionnĆ© dans "localStorage". Si aucun n'est sĆ©lectionnĆ©, utilise la valeur par dĆ©faut.
// S'il y a un thĆØme dans localStorage, on l'utilise et on l'applique.
if (whichTheme) {
$("head").append("<link href='" + activeTheme + '?version=' + string + "' type=\"text/css\" rel=\'stylesheet\' />");
}
else {
// No need to include anything here as there's no CSS to add.
// Pas besoin d'inclure quoi que ce soit ici car il n'y a pas de CSS Ć ajouter.
}
$('ul#logged-in-menu').prepend(panel);
$('ul#logged-out-menu').prepend(panel);
if (utils.findBootstrapEnvironment()==='xs') {
$('#menu').prepend(panel);
}
$(document).ready(function () {
// Listen to the NAV dropdown for any changes
// Ćcoute la liste dĆ©roulante NAV pour tout changement de thĆØme
$("#theme li a").on("click change", function () {
// If we detect a change, append the selected CSS file into the DOM
// Si un changement est dƩtectƩ, on ajoute le fichier CSS sƩlectionnƩ dans le DOM (Document Object Model)
var thishref=$(this).attr('rel');
$("link[rel=stylesheet]").attr('href', thishref + "?version=" + string + "");
//location.reload();
//$("head").append("<link href='" + $(this).attr("rel") + $(this).attr("id") + " type=\'text/css\' rel=\'stylesheet\' />");
location.reload();
// This variable stores the selected theme ID
// Cette variable stocke l'ID du thĆØme sĆ©lectionnĆ©
var selected=$(this).attr("id");
// This variable stores the selected theme link
// Cette variable stocke le lien du thĆØme sĆ©lectionnĆ©
var theTheme=$(this).attr("rel");
// This variable updates the selected theme ID
// See if "default" has been selected. If it has, then...
// Cette variable met Ć jour l'ID du thĆØme sĆ©lectionnĆ©
// Regarde si "default" a ƩtƩ sƩlectionnƩ. Si c'est le cas, alors...
if (selected==='default') {
localStorage.setItem("theme", "");
// This variable will strip the current appeneded theme ID
// Cette variable supprimera l'ID du thĆØme actuellement ajoutĆ©
localStorage.setItem("activeTheme", "");
// This variable will strip the current appeneded theme URL (HREF)
// Finally, we have to reload the page to effect the changes
// Cette variable supprimera l'URL actuelle du thĆØme ajoutĆ© (HREF)
// Enfin, on recharge la page pour effectuer les modifications
location.reload();
}
// If any other theme is selected, carry on as normnal, and update localStorage
// Si un autre thĆØme est sĆ©lectionnĆ©, continuez normalement et mettez Ć jour localStorage
else {
localStorage.setItem("theme", selected);
// This variable updates the actual href of the CSS file
// Cette variable met Ć jour le href rĆ©el du fichier CSS
localStorage.setItem("activeTheme", theTheme);
//window.location.href = window.location.href
}
// We use return false to prevent the browser from reloading or following any HREF links
// On utilise la fonction "return false" pour empĆŖcher le navigateur de recharger ou de suivre les liens HREF
//return false;
}
);
}
);
}
);
// When hovering over the #switcher element, target the i class and add 'themeoff'
// Lorsque du survol de l'ƩlƩment #switcher, on cible la classe CSS "i" et on ajoute le CSS "themeoff"
$(document).on('mouseenter', '#switcher', function() {
$('#switcher i').addClass("themeoff");
}
);
// When leaving the however state, target the i class and remove 'themeoff'
// Lorsque l'on quitte l'Ć©tat, on cible la classe CSS "i" et on supprime le CSS "themeoff"
$(document).on('mouseleave', '#switcher', function() {
$('#switcher i').removeClass("themeoff");
}
);