78 lines
2.2 KiB
JavaScript
78 lines
2.2 KiB
JavaScript
|
|
// Apply the chosen theme, and save that choice in a cookie on the user's browser
|
|
//
|
|
function SelectTheme(which) {
|
|
reloadCSS(which);
|
|
setCookie('theme', which, 10); // Remember which theme they like
|
|
}
|
|
|
|
// Apply the CSS, which hosts the theme.
|
|
//
|
|
function reloadCSS(which) {
|
|
var links = document.getElementsByTagName("link");
|
|
for (var cl in links) {
|
|
var link = links[cl];
|
|
if (link.rel === "stylesheet")
|
|
link.href = "inc/theme-" + which + ".css";
|
|
}
|
|
}
|
|
|
|
//
|
|
//
|
|
function includeHTML() {
|
|
var z, i, elmnt, file, xhttp;
|
|
/* Loop through a collection of all HTML elements: */
|
|
z = document.getElementsByTagName("*");
|
|
for (i = 0; i < z.length; i++) {
|
|
elmnt = z[i];
|
|
/*search for elements with a certain atrribute:*/
|
|
file = elmnt.getAttribute("w3-include-html");
|
|
if (file) {
|
|
/* Make an HTTP request using the attribute value as the file name: */
|
|
xhttp = new XMLHttpRequest();
|
|
xhttp.onreadystatechange = function() {
|
|
if (this.readyState == 4) {
|
|
if (this.status == 200) {elmnt.innerHTML = this.responseText;}
|
|
if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
|
|
/* Remove the attribute, and call this function once more: */
|
|
elmnt.removeAttribute("w3-include-html");
|
|
includeHTML();
|
|
}
|
|
}
|
|
xhttp.open("GET", file, true);
|
|
xhttp.send();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Save the cookie on the user's browser - this is for the color theme
|
|
// and it has a timeout of some number of days, after which it expires.
|
|
//
|
|
function setCookie(cname, cvalue, exdays) {
|
|
const d = new Date();
|
|
d.setTime(d.getTime() + (exdays*24*60*60*1000));
|
|
let expires = "expires="+ d.toUTCString();
|
|
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
|
|
}
|
|
|
|
// Try to get the cookie. If the user has one, then it will use that
|
|
// to apply a specific theme.
|
|
//
|
|
function getCookie(cname) {
|
|
let name = cname + "=";
|
|
let decodedCookie = decodeURIComponent(document.cookie);
|
|
let ca = decodedCookie.split(';');
|
|
for(let i = 0; i <ca.length; i++) {
|
|
let c = ca[i];
|
|
while (c.charAt(0) == ' ') {
|
|
c = c.substring(1);
|
|
}
|
|
if (c.indexOf(name) == 0) {
|
|
return c.substring(name.length, c.length);
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|