Go Back   Gaming Gutter > Non-Gaming > Programming > Tutorials


Tutorials - Looking for programming tutorials to increase your knowledge? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post So guys...
Today 07:02 PM
Last post by Snakebite
Today 07:43 PM
12 Replies, 33 Views
Go to first new post f*ck
Today 06:41 PM
Last post by Hodizzle
Today 07:42 PM
18 Replies, 51 Views
Go to first new post SOTW #1 Winner
Today 02:19 PM
Last post by Hodizzle
Today 07:37 PM
2 Replies, 14 Views
Go to first new post Where...
Today 06:37 PM
by Saint
Last post by Oraski
Today 07:36 PM
2 Replies, 12 Views
Go to first new post How....
Today 05:30 PM
Last post by Check
Today 07:24 PM
8 Replies, 41 Views
Reply
 
LinkBack Thread Tools Display Modes

 Alternate Style Sheets [CSS + JS]
Old 10-18-2006, 03:56 PM   #1 (permalink)
Oosband
Guest

 
Posts: n/a
iTrader: / %
Alternate Style Sheets [CSS + JS]

Cant decide on a look for your webste?
Well if you have coded the whole thing in CSS then you can use this nifty little feature to let the user change layouts.

In the head section of your page, call for your default style sheet and all your alternate ones;

Code:
<link rel="stylesheet" type="text/css" href="default.css" title="default" />
<link rel="alternate stylesheet" type="text/css" href="style2.css" title="style2" />
<link rel="alternate stylesheet" type="text/css" href="style3.css" title="style3" />
<link rel="alternate stylesheet" type="text/css" href="style4.css" title="style4" />
<script type="text/javascript" src="styles.js"></script>
Now, for your links to switch the styles, you simply add somthing like this to your page;

The last part of this little tutorial is the javascript, it changes the style sheet and remembers which style you prefer when you go back to the page, using cookies.

Save this as styles.js;

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);
Have fun!
  Reply With Quote

 
Old 10-31-2006, 05:09 AM   #2 (permalink)
unlimitedorb
Guest

 
Posts: n/a
iTrader: / %
Nice, but it would be helpful to have well commented code as well.
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Style sloop Website Suggestions 6 12-06-2006 03:33 AM

Powered by vBadvanced CMPS v3.0 RC2

All times are GMT -7. The time now is 07:46 PM.


vBulletin skin developed by: eXtremepixels
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.13653803 seconds (100.00% PHP - 0% MySQL) with 20 queries