Last active
November 26, 2021 16:15
-
-
Save jmwhittaker/4540000 to your computer and use it in GitHub Desktop.
Standard compliant stylesheet switcher for HTML5. Works on iOS5 and all modern browsers.
This file contains hidden or 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
$(document).ready(function() | |
{ | |
/* Check to see if we have saved a style already, a bit verbose but you get the drift! */ | |
var theme = localStorage.getItem('style'); | |
if (!theme) { | |
localStorage.setItem('style', 'light'); | |
}; | |
updateTheme(); | |
$('.js-change-style').click(function() | |
{ | |
theme = localStorage.getItem('style'); | |
if (theme === 'light') { | |
theme = 'dark'; | |
} else { | |
theme = 'light'; | |
} | |
localStorage.setItem('style', theme); | |
updateTheme(); | |
return false; | |
}); | |
} | |
function updateTheme() | |
{ | |
currentTheme = localStorage.getItem('style'); | |
$('meta[http-equiv=Default-Style]')[0].content = currentTheme; | |
} | |
This file contains hidden or 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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="Default-Style" content=""> | |
<title>Stylesheet Switcher</title> | |
<!-- CSS --> | |
<link rel="stylesheet" href="/styles/darktheme.css" title="dark"> | |
<link rel="stylesheet" href="/styles/lighttheme.css" title="light"> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
<script src="app.js"></script> | |
</head> | |
<body> | |
<h1>Hello World</h1> | |
<a href="#" class="js-change-style">change style</a> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to implement this code, but it's not working. I can get the js to correctly modify the meta tag, but this does not change the stylesheet in the browser. Alternatively I can use the View > Page Syle menu in Firefox correctly. I'm not sure what the problem is.. can you suggest anything? I've tried pasting your code verbatim and that still doesn't work.