php forum
php mysql forum
php mysql smarty
 
Topic Options
#109860 - 01/08/04 09:34 PM Style/Template Changer help
chuggs93 Offline
Junior Member

Registered: 12/11/03
Posts: 13
Im using this code for a style changer

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);
In my head section i have this
Code:
<link rel="stylesheet" type="text/css" href="tcw.css" title="default"/>
<link rel="alternate stylesheet" type="text/css" href="tcw1.css" title="tcw1"/>
Im supposed to be using HREF's to make this work.
Code:
<a href="#" 
onclick="setActiveStyleSheet('default'); 
return false;">change style to default</a><a href="#" 
onclick="setActiveStyleSheet('tcw1'); 
return false;">change style to paul</a> 
and it does work. However, how do i convert these to use DROPDOWN's instead of hrefs?

Top
#109861 - 01/09/04 06:37 AM Re: Style/Template Changer help
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
try this:

Code:
<script>
function css_drop (select) {
   var value = select.options[select.selectedIndex].value;
   if(value) {
      setActiveStyleSheet(value);
      return false;
   }
   select.selectedIndex = 0;
}
</script>

<form>
<select name="css_drops" onchange="css_drop(this)">
<option>Select a style</option>
<option value="default">change style to default</option>
<option value="tcw1"   >change style to paul</option>
</select>
</form>
_________________________
one bytecode to rule them all

Top
#109862 - 01/09/04 09:17 AM Re: Style/Template Changer help
chuggs93 Offline
Junior Member

Registered: 12/11/03
Posts: 13
It works, thanks alot!

Top
#109863 - 01/09/04 01:27 PM Re: Style/Template Changer help
chuggs93 Offline
Junior Member

Registered: 12/11/03
Posts: 13
Is there a way to have a go button or something, because i need it to auto refresh the page..

Top
#109864 - 01/09/04 03:49 PM Re: Style/Template Changer help
Charles Capps Offline
Admin Emeritus

Registered: 01/09/00
Posts: 5438
Loc: Lynnwood, WA
Oh, that's going to be a problem...

When the page reloads, it will default back to whatever the original stylesheet was, forgetting the user choice.

In order to remember the user stylesheet pref, you'd probably want to set a cookie, then use a document.write to set the default stylesheet based on that cookie. Of course, you don't want to store the stylesheet name with the cookie, just an identifier to associate with the stylesheet, as to avoid forged cookies causing problems...

Confused? %)
_________________________
UBB.classic: Love it or hate it, it was mine.

Top



Latest Posts
[7.2.1] - Naked shoutbox
by bellaonline
05/05/12 05:00 PM
[7.x] Stop Forum Spam Integration v0.4
by bellaonline
05/05/12 03:53 PM
Shout Box

(Views)Popular Topics
Known public proxy servers 1689885
Integrated Index Page (IIP) 5.3.1 555705
Finished-[6.5.2] Games Arcade Deluxe v1.9 501236
Integrated Index Page (IIP) 5.1.1 415112
TLD Bv2.1 Released - Threads Links Directory 396822
[6.0x] Who's Online 4.0.0 [Finished] 389412
Finished-[6.5.1] Integrated Index Page (IIP) 6.5 330423
Q & A 298663
Slash UBB 266936
[6.3.x] [beta] Hit Hack 2.0 227970
Forum Stats
13621 Members
59 Forums
37191 Topics
295716 Posts

Max Online: 686 @ 06/28/07 07:04 AM

 

 

 
fusionbb message board php hacks