I'm looking for some light-weight JS code that can swap style-sheets by onClick of a button. I'm currently using one that only seems to work properly in Firefox. My goal is to have the visitor hit a "lightswitch" and swap style-sheets to go from a very light style page, to one thats very dark. Other then it only working in firefox, the JS code I'm using now works pretty good. My only gripes are that it reloads the default style-sheet on refresh and it works off of two a href links.
I've used a version of this theme switcher forever. Instead of disabling the stylesheet as you have above, it just disables the link, it works very nicely.
Actually, the one of dynamic drive is really messy... this is the version that I used in the past, now I just use jQuery to disable the link.
function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }
function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; }
function setActiveStyleSheet(title) { var i, a, main; for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) { a.disabled = true; if(a.getAttribute("title") == title) { a.disabled = false; createCookie("style", title, 365); } } } }
function setPassiveStyleSheet(title) { var i, a, main; for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) { a.disabled = true; if(a.getAttribute("title") == title) a.disabled = false; } } }
Note: The setPassiveStylesheet function is run on page load, then the setActiveStyleSheet function is used to change themes. The title is just the title of the theme contained within the link.
Cool, looks like I have some things to try out. Is the script you provided triggered by a clicking a link Mottie? Im not that great at jquery or .js just yet.
I'm looking for some light-weight JS code that can swap style-sheets by onClick of a button. I'm currently using one that only seems to work properly in Firefox. My goal is to have the visitor hit a "lightswitch" and swap style-sheets to go from a very light style page, to one thats very dark. Other then it only working in firefox, the JS code I'm using now works pretty good. My only gripes are that it reloads the default style-sheet on refresh and it works off of two a href links.
Heres the JS code and style-sheet links:
Heres the mark up for the "switch":
You can check out the code above in action here.
Keep in mind that the page layout in the link ONLY works in Firefox right now.
Thanks for any Input!
-Aaron-
It basically just changes the class of the BODY element, so you can have separate sections in your CSS file that only apply to specific body classes.
Actually, the one of dynamic drive is really messy... this is the version that I used in the past, now I just use jQuery to disable the link.
Set up the links like this:And the script:Note: The setPassiveStylesheet function is run on page load, then the setActiveStyleSheet function is used to change themes. The title is just the title of the theme contained within the link.