HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Simple style switcher - tutorial

422422 Developer MVP
edited November 2011 in Feedback
Thought I would create a thread for this, feel free to use however you wish.

Please test this locally , you will need to create index.html page add this code to the head
<link href="css/main.css" rel="stylesheet" type="text/css" title="main" media="screen" /> <link href="css/alt1.css" rel="alternate stylesheet" type="text/css" title="alt1" media="screen" /> <link href="css/alt2.css" rel="alternate stylesheet" type="text/css" title="alt2" media="screen" /> <script type="text/javascript" src="js/styleswitcher.js"></script>

In the body of the index page add this code:
<a href="javascript:;" onclick="setActiveStyleSheet('main'); return false;">change style to default</a> <a href="javascript:;" onclick="setActiveStyleSheet('alt1'); return false;">Alternate style 1</a> <a href="javascript:;" onclick="setActiveStyleSheet('alt2'); return false;">Alternate style 2</a>

Create a folder called css and one called js

In the css folder create 3 files. alt1.css , alt2.css and main.css ( create different css for each of these files and save )

in the js folder create a file called styleswitcher.js

in that file add this code.
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; } } } function getActiveStyleSheet() { var i, a; for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title"); } return null; } function getPreferredStyleSheet() { var i, a; for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title") ) return a.getAttribute("title"); } return null; } 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; } window.onload = function(e) { var cookie = readCookie("style"); var title = cookie ? cookie : getPreferredStyleSheet(); setActiveStyleSheet(title); } window.onunload = function(e) { var title = getActiveStyleSheet(); createCookie("style", title, 365); } var cookie = readCookie("style"); var title = cookie ? cookie : getPreferredStyleSheet(); setActiveStyleSheet(title);

Now add some content to your index.html page, that will be styled. And test it out. With a little help you can make this work with your forums I am sure ( i havent checked the code but should be simple enough )

There was an error rendering this rich post.

Comments

Sign In or Register to comment.