Skip to content

Instantly share code, notes, and snippets.

@ryanve
Last active November 12, 2017 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanve/e31d6cd97ba14fe0421ee237493ce7ff to your computer and use it in GitHub Desktop.
Save ryanve/e31d6cd97ba14fe0421ee237493ce7ff to your computer and use it in GitHub Desktop.
swap stylesheet via <select> and save choice in storage
!function() {
var handle = "useColor";
var storage = window.sessionStorage;
var href = storage && storage.getItem(handle) || "";
var control = document.getElementById(handle);
if (!control) throw new Error("Unused code.");
var head = document.head;
var link = document.createElement("link");
link.rel = "stylesheet";
control.value = link.href = href;
control.addEventListener("change", function() {
href = control.value;
load(href);
if (storage) storage.setItem(handle, href);
});
load(href);
function load(href) {
var isNone = !href;
var inHead = link.parentNode === head;
if (isNone) {
link.removeAttribute("href");
inHead && head.removeChild(link);
} else {
link.href = href;
inHead || head.appendChild(link);
}
}
}();
<label class="preset-box block-table mt1">
  <code class="mr1">color</code>
  <select id="useColor" class="shadow-ring">
    <option value>none</option>
    <option value="../color/main.css?" data-hidden="local">local</option>
    <option value=".............../color/master/main.css?">VPN</option>
  </select>
</label>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment