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
» GG Stuff

Follow us on Twitter!

Get the GG toolbar today (for firefox only)
» Recent Threads
Free XBox 360 Avatar...
Today 01:32 PM
by Coby
Last post by Coby
Today 01:32 PM
0 Replies, 1 Views
Go to first new post Got iced after logging...
02-16-2010 12:15 AM
by Fury
Last post by Avatar
Today 01:31 PM
24 Replies, 556 Views
Go to first new post The Official Neopets...
03-10-2010 05:05 PM
by Boink
Last post by Avatar
Today 01:26 PM
14 Replies, 202 Views
Go to first new post Hash Listing @ Its Best!
02-08-2010 09:54 PM
Last post by Avatar
Today 01:26 PM
46 Replies, 1,016 Views
Go to first new post ~ The Official Hash...
11-06-2009 11:56 AM
by mehike
Last post by Kiotrama
Today 01:25 PM
875 Replies, 19,687 Views
Reply
 
LinkBack Thread Tools Display Modes

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

 
Posts: n/a
GPoints: 0 [Check]
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, 06:09 AM   #2 (permalink)
unlimitedorb
Guest

 
Posts: n/a
GPoints: 0 [Check]
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 04:33 AM

Powered by vBadvanced CMPS v3.1.0

All times are GMT -7. The time now is 01:43 PM.


vBulletin skin developed by: eXtremepixels
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

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