<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>
Last active
November 12, 2017 20:03
-
-
Save ryanve/e31d6cd97ba14fe0421ee237493ce7ff to your computer and use it in GitHub Desktop.
swap stylesheet via <select> and save choice in storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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); | |
} | |
} | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment