Previous Thread
Next Thread
Print Thread
Rate Thread
#109860 01/08/2004 10:34 PM
Joined: Dec 2003
Posts: 13
Junior Member
Junior Member
Offline
Joined: Dec 2003
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?

Sponsored Links
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
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>

Joined: Dec 2003
Posts: 13
Junior Member
Junior Member
Offline
Joined: Dec 2003
Posts: 13
It works, thanks alot!

Joined: Dec 2003
Posts: 13
Junior Member
Junior Member
Offline
Joined: Dec 2003
Posts: 13
Is there a way to have a go button or something, because i need it to auto refresh the page..

Joined: Jan 2000
Posts: 5,073
Admin Emeritus
Admin Emeritus
Joined: Jan 2000
Posts: 5,073
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.
Sponsored Links

Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Shock Hosting
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
AllenAyres
AllenAyres
Texas
Posts: 21,080
Joined: March 2000
Forum Statistics
Forums63
Topics37,583
Posts293,955
Members13,824
Most Online151,614
Nov 14th, 2025
Today's Statistics
Currently Online 701
Topics Created 0
Posts Made 0
Users Online 0
Birthdays 13
Top Posters
AllenAyres 21,080
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,834
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2026 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.1.0
(Snapshot build 20260108)