/    Sign up×
Community /Pin to ProfileBookmark

Cycling through style sheets?

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.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@NatdripJan 06.2004 — HI ASHY

THIS MAY HELP ?

[URL=http://lists.w3.org/Archives/Public/www-style/1998Sep/0041.html]lists.w3.org/Archives/Public/www-style/1998Sep/0041.html[/URL]
Copy linkTweet thisAlerts:
@AshleyQuickauthorJan 06.2004 — That thread was never resolved. Thanks anyway. ?

Can someone help??

Ashley
Copy linkTweet thisAlerts:
@Khalid_AliJan 07.2004 — 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
Copy linkTweet thisAlerts:
@AshleyQuickauthorJan 07.2004 — I don't understand where you add those variables? Can you assist further? Sorry. ?
×

Success!

Help @AshleyQuick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...