Click to See Complete Forum and Search --> : Cycling through style sheets?


AshleyQuick
01-06-2004, 01:04 PM
I'm trying to modify this script so that, vs. having individual links for each of the four style sheets I want to use, I can use one href that will cycle through all of them as you keep clicking. Please help!

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

Currently, it will only work using a separate link for each sheet:

<a href="#"
onclick="setActiveStyleSheet('style01');
return false;">change style to style01</a>

<a href="#"
onclick="setActiveStyleSheet('style02');
return false;">change style to style02</a> ...etc.

Natdrip
01-06-2004, 02:04 PM
HI ASHY

THIS MAY HELP :confused:

lists.w3.org/Archives/Public/www-style/1998Sep/0041.html (http://lists.w3.org/Archives/Public/www-style/1998Sep/0041.html)

AshleyQuick
01-06-2004, 02:14 PM
That thread was never resolved. Thanks anyway. :)

Can someone help??

Ashley

Khalid Ali
01-06-2004, 06:06 PM
decalre a global variable may be say
ctr=0;

now it looks like the style sheets have smae name only that the last letter is different

01,02
I am thinking there is
00 and 04
anyways whatever is the first number set the ctr=to that value if its 0
ctr=0;

now in the function call make it something like this
<a href="#"
onclick="setActiveStyleSheet(ctr++);
return false;">change style to style02</a>

in the actual function create the stylesheet name with this

such as
var styleName = "style0"+ctr+".css";

and then do the remaining stuff,
Logically it will work

AshleyQuick
01-07-2004, 08:47 AM
I don't understand where you add those variables? Can you assist further? Sorry. :(