document.addEventListener("DOMContentLoaded", function(event) {
// Check for preference or previous setting.
checkUserPreference();
});
// Get the select element.
const selectEle = document.getElementById('selectSchema');
// Watch the select element for change.
selectEle.addEventListener('change', function() {
themeSchemaSetting();
});
// Check for user preference.
function checkUserPreference() {
// Check to see if storage is set if not initialise.
if (!localStorage.hasOwnProperty('currentSchema')) {
const prefersDarkSchema = window.matchMedia("(prefers-color-scheme: dark)");
const prefersLightSchema = window.matchMedia("(prefers-color-scheme: light)");
// If user preference detected set system schema, else set light schema.
if (prefersDarkSchema.matches || prefersLightSchema.matches) {
selectEle.selectedIndex = 0; // Set system in select.
} else {
selectEle.selectedIndex = 2; // Set light in select.
}
}
// Set the schema.
themeSchemaSetting();
}
// Set the schema.
function themeSchemaSetting(currentSchema) {
// Set & retreive local storage.
selectValue = selectEle.value;
localStorage.setItem('currentSchema', selectValue);
currentSchema = localStorage.getItem('currentSchema');
// debug log
console.log("Select Value:", selectValue);
console.log("Storage Schema:", currentSchema);
// set theme based on set storage
switch (currentSchema) {
case 'systemSchema':
// set light schema.
console.log("Theme is system.");
break;
case 'lightSchema':
// set dark schema.
console.log("Theme is Light.");
break;
case 'darkSchema':
// let the system decide.
console.log("Theme is Dark.");
break;
default:
// default schema developer preference.
console.log("Theme is default. (Light)");
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.