SMF For Free Support Forum
Signup For Free Forum
July 05, 2008, 05:58:56 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome to SMF For Free. The best free SMF Host
 
   Home   Help Search Arcade Gallery Login Register  

Pages: [1] 2 3 4 5
  Print  
Author Topic: [Code] Stylesheet swapper for Portal Block  (Read 1951 times)
0 Members and 1 Guest are viewing this topic.
simply sibyl
Global Moderator
*
Offline Offline

Posts: 6832



View Profile WWW
« on: February 09, 2008, 09:44:51 am »

Add a portal block which allows your members to choose stylesheets.

Tested for several months, in IE, Firefox, SeaMonkey, Konquerer, and Opera.


Live Demo

There are several steps.  Make sure you follow each one of them carefully.
(Suggestion:   Print this page)

Step 1: The Stylesheets

Decide which stylesheet you are going to use as your forums default style. 
Add it to your forum in Manage Styles and set it as default.  (choose it with the style picker)

Find other styles you want to use. 
Add each of them in Manage Styles. 
When adding them name them the names you want your members to see in a list when they choose them.   
(will help you to remember them when setting this up if you write the names down)

There are dozens of stylesheets, and the directions to add them to your forum here:
Stylesheet Index
Holiday Stylesheets

Step 2: The Stylesheet Code
(You are going to edit this)

The FIRST link in the code (link rel="stylesheet") = The stylesheet you chose to be the default style for your forum.
The SECOND link (link rel="alternate stylesheet") =  Additional stylesheet.

Code:

<!-- Stylesheet Swapper For Portal Block  -->
<link rel="stylesheet" type="text/css" href="http://www.smfforfree3.com/styles/sibylstestsite/enhanced.css?fin11" title="TITLE">

<link rel="alternate stylesheet" type="text/css" href="http://www.smfforfree3.com/styles/sibylstestsite/carbonfiber.css?fin11" title="TITLE">


Add more of these for each alternate stylesheet:

Code:

<link rel="alternate stylesheet" type="text/css" href="http://www.smfforfree3.com/styles/sibylstestsite/carbonfiber.css?fin11" title="TITLE">


How to edit the code:




1.  YELLOW:  Replace smfforfree3.com with YOUR forums info.
(for instance smfforfree.com, smfforfree2.com, smfforfree3.com, smfforfree4.com, mysmf.com, freesmfhosting.com)
Make certain you have this correct or it will NOT work.

2.  GREEN:  Replace sibylstestsite with the name of your forum.

3.  BLUE:    Replace enhanced with the name of your stylesheet.
 
4.  PINK:    Replace TITLE with the word you want your members to see as the choice in the portal menu  (lower case - no spaces)

Do this for all of the  stylesheets you are going to use.

After making the edits put the code at the top of your HEADER.

You are going to need all of the title names when you edit the code for the portal block.
Either write them down or keep another window open in your browser.   
It is important that the names match in both places.

Step 3:  The Script  (swaps stylesheet, sets cookie, radio selection)

Put this right underneath the stylesheet code in your HEADER.
IMPORTANT:  Do NOT change anything in the script

Code:
<!-- Script for Stylesheet Swapper DO NOT EDIT  -->
<script type='text/javascript'>
function changeStyle(title) {
var lnks = document.getElementsByTagName('link');
for (var i = lnks.length - 1; i >= 0; i--) {
if (lnks[i].getAttribute('rel').indexOf('style')> -1 && lnks[i].getAttribute('title')) {
lnks[i].disabled = true;
if (lnks[i].getAttribute('title') == title) lnks[i].disabled = false;
}}}


function switchit() {
if (readCookie('style')) { title=readCookie('style'); changeStyle(title); var radios = document.getElementById ('styleswitcher');
if (radios) {
 var inputs = radios.getElementsByTagName ('input');
 if (inputs) {
   for (var i = 0; i < inputs.length; ++i) {
     if (inputs[i].type == 'radio' && inputs[i].name == 'style')
       inputs[i].checked = inputs[i].value == (title)
   }
 }
} } }


// function topchangeStyle(title) {
// var lnks = document.getElementsByTagName('link');
// for (var i = lnks.length - 1; i >= 0; i--) {
// if (lnks[i].getAttribute('rel').indexOf('style')> -1 && lnks[i].getAttribute('title')) {
// lnks[i].disabled = true;
// if (lnks[i].getAttribute('title') == title) lnks[i].disabled = false;
// }}}




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);
</script>


Step 4: Footer Code makes radio buttons persist (FF,SM,Opera,Konquerer)

Place this code in your Footer.
 
Code:

<body onload="switchit()">




Step 5:  Set up the Portal Block

Make an html block ... name it whatever you wish.
Edit the html block.

The code below will go in the block.
Ive separated it with spaces to help you to understand it.

There is a section for each stylesheet.
 
For EACH stylesheet  do the following:
Remember to put in a choice for your default style too.

1.  change TITLE NAME to the exact same TITLE name you used in the header  (important)
     (reminder:   lowercase - no spaces)

2.  change NAME THEY SEE IN THE LIST to the word they will see in the list of choices.
    (you can name it as you wish)

Note:   I have the width of the table set to 210 as it works well on my forum that way.
You can change that to accomodate your own portal.


Code:
<style type="text/css">
fieldset { border: none !important; }
</style>

<table align=center width="210px" cellspacing="5">
<td align=left valign="top">
<form action="#" id="styleswitcher" class="styleswitcher"><small>

<input type="radio" name="style" value="TITLE NAME" onclick='changeStyle(this.value); parent.frames["top"].changeStyle(this.value)'> NAME THEY SEE IN THE LIST

<br>

<input type="radio" name="style" value="TITLE NAME" onclick='changeStyle(this.value); parent.frames["top"].changeStyle(this.value)'> NAME THEY SEE IN THE LIST


</fieldset>
</form>
<a href="http://sibylstestsite.smfforfree3.com/index.php"><u>swapper code by simply sibyl</u></a>
</td>
</table>

Add/edit more of these for each additional stylesheet:

Code:

<input type="radio" name="style" value="TITLE NAME" onclick='changeStyle(this.value); parent.frames["top"].changeStyle(this.value)'> NAME THEY SEE IN THE LIST




thanks to:
http://www.washington.edu/webinfo/snippets/javascript/formvals.html for info on how to automagically select radio buttons
http://javascript.about.com/library/blswitch.htm for how to on using js to switch stylesheets
http://www.quirksmode.org/js/cookies.html for cookie information

« Last Edit: February 09, 2008, 12:44:37 pm by simply sibyl » Logged

LaundryLady
Global Moderator
*
Offline Offline

Posts: 2773


Keep Smiling, Let'em Wonder!


View Profile WWW
« Reply #1 on: February 09, 2008, 09:57:59 am »

Looks really great, Sibyl.  I can't wait to put this up after I finish my current project.
Logged

Pancakes
SMF For Free Sr. Member
*
Offline Offline

Posts: 457


sǝʞɐɔuɐd ǝɯ lol


View Profile
« Reply #2 on: February 09, 2008, 11:39:19 am »

Aw didn't work on my skin test forum
Logged

simply sibyl
Global Moderator
*
Offline Offline

Posts: 6832



View Profile WWW
« Reply #3 on: February 09, 2008, 11:43:28 am »

forum url?    If you do not want to post the link to your test forum pm it to me so I can take a look.

Go back thru the guide, each step of it.  Double check everything you did to be sure its all correct.
Logged

Agent Moose
Moderator
*****
Offline Offline

Posts: 611


Do not PM me for Code Support or Request


View Profile WWW
« Reply #4 on: February 09, 2008, 11:53:07 am »

Woah!  Very very nice Smiley
Logged


SMC Code Index V3 - The best Code Index for all your Needs!  Includes a bunch of codes not on Support.
simply sibyl
Global Moderator
*
Offline Offline

Posts: 6832



View Profile WWW
« Reply #5 on: February 09, 2008, 11:53:34 am »

1.  For each stylesheet the TITLE in both the header and portal block have to be the same.
    (lower case - no spaces)

2.  Make sure the stylesheet link address in the header code is correct.
    Easy way to find that out is to put the link in your browsers address bar and go to it.
    If it gives you the option to download the css file then you know you have the correct address.
Logged

simply sibyl
Global Moderator
*
Offline Offline

Posts: 6832



View Profile WWW
« Reply #6 on: February 09, 2008, 11:54:12 am »

Woah!  Very very nice Smiley

Thanks Moose - been using it for months.  Figured it was time to share  Wink
Logged

LaundryLady
Global Moderator
*
Offline Offline

Posts: 2773


Keep Smiling, Let'em Wonder!


View Profile WWW
« Reply #7 on: February 19, 2008, 10:03:10 am »

Well, I have tried it again, and I still can't make it work.
Logged

simply sibyl
Global Moderator
*
Offline Offline

Posts: 6832



View Profile WWW
« Reply #8 on: February 19, 2008, 10:07:16 am »

I'm not sure which test site you are using..    toss me a note and I'll take a look?
Logged

simply sibyl
Global Moderator
*
Offline Offline

Posts: 6832



View Profile WWW
« Reply #9 on: February 19, 2008, 10:09:18 am »

nevermind.. I see its on your main site.. will check it out
Logged

simply sibyl
Global Moderator
*
Offline Offline

Posts: 6832



View Profile WWW
« Reply #10 on: February 19, 2008, 10:15:38 am »

LL - did you choose "aqua" with the stylepicker and choose that stylesheet as the default?
Logged

LaundryLady
Global Moderator
*
Offline Offline

Posts: 2773


Keep Smiling, Let'em Wonder!


View Profile WWW
« Reply #11 on: February 19, 2008, 10:17:38 am »

Dont know what I did any more.  Taking it all off for now.  Too dang frustrating to figure out.
Logged

simply sibyl
Global Moderator
*
Offline Offline

Posts: 6832



View Profile WWW
« Reply #12 on: February 19, 2008, 10:24:12 am »

don't!
It does work..   Im sending you a pm.   I set it up on my test site using what you put in and its working but I need you to try one thing..
Logged

LaundryLady
Global Moderator
*
Offline Offline

Posts: 2773


Keep Smiling, Let'em Wonder!


View Profile WWW
« Reply #13 on: February 19, 2008, 10:54:47 am »

Bless your heart, that was it.  It was an earlier code I had saved.
Logged

simply sibyl
Global Moderator
*
Offline Offline

Posts: 6832



View Profile WWW
« Reply #14 on: February 19, 2008, 10:57:43 am »

Bless your heart, that was it.  It was an earlier code I had saved.

Wink   glad its working now
Logged



Pages: [1] 2 3 4 5
  Print  
 
Jump to:  

Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC
ServerBeach Coupon
Page created in 0.261 seconds with 17 queries.