By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,817 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,817 IT Pros & Developers. It's quick & easy.

Dynamically changing a stylesheet

P: n/a
I have a piece of javascript I need to modify. Right now it changes a
stylesheet in the document between style.css and no_indent.css. These
are in the head of my document:

<link rel=stylesheet href=/style.css>
<link rel=stylesheet href=/no_indent.css>
<link rel=stylesheet href=/style.css>
What the code below does is toggle between the two depending on what
link is clicked. Here are the links:

<a href=javascript:changeSheets(1)>Threaded</a>
<a href=javascript:changeSheets(2)>Flat</a>

What I want to do is have one link that will toggle between the two
stylesheets. So the page loads style.css initially. If the link is
clicked once it changes to no_indent.css. If it's clicked again it
changes back to style.css, and so on.

Can anyone let me know how to do this?

TIA

code:
----------------------------------

function changeSheets(whichSheet){
whichSheet=whichSheet-1;
if(document.styleSheets){
var c=document.styleSheets.length;
for(var i=0;i<c;i++){
if(i!=whichSheet){
document.styleSheets[i].disabled=true;
}else{
document.styleSheets[i].disabled=false;
}
}
}
}
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> What I want to do is have one link that will toggle between the two
stylesheets. So the page loads style.css initially. If the link is
clicked once it changes to no_indent.css. If it's clicked again it
changes back to style.css, and so on.


How about leaving the links as they are but give each an id and, using the
style sheets, position them in the same space.
Then toggle their visibility in the function you call.
Jul 20 '05 #2

P: n/a
wjbell wrote:
I have a piece of javascript I need to modify. Right now it changes a
stylesheet in the document between style.css and no_indent.css. These
are in the head of my document:

<link rel=stylesheet href=/style.css>
<link rel=stylesheet href=/no_indent.css>
<link rel=stylesheet href=/style.css>
This is invalid HTML. Attribute values containing `/' must be
delimited using doule or single quotation marks. Since this
is not the only exception, you should always delimit attribute
values that way.
What the code below does is toggle between the two depending on what
link is clicked. Here are the links:

<a href=javascript:changeSheets(1)>Threaded</a>
<a href=javascript:changeSheets(2)>Flat</a>
Not only this is invalid HTML again because of omitted quotation
marks, it is also a Bad Thing. What about users without client-side
JavaScript support? You should write the hyperlinks dynamically (so
only if JavaScript is supported) and use the `onclick' event handler
where supported:

<script type="text/javascript">
<!-- Use the SGML comment in HTML only

var a = ["Threaded", "Flat"];
var s = "";

for (var i = 0; i < a.length; i++)
{
s +=
'<a href="javascript:changeSheets(' + (i + 1) + ')"'
' onclick="changeSheets(' + (i + 1) + '); return false;">'
+ a[i]
+ '<\/a>\n';
}

document.write(s);

// Use the SGML comment in HTML only -->
</script>
What I want to do is have one link that will toggle between the two
stylesheets. So the page loads style.css initially. If the link is
clicked once it changes to no_indent.css. If it's clicked again it
changes back to style.css, and so on. Can anyone let me know how to
do this?


Quickhack:

<script type="text/javascript">
<!-- Use the SGML comment in HTML only

var currentSheet = 0;
var s =
'<a href="javascript:changeSheet(!currentSheet)"'
+ ' onclick="changeSheet(!currentSheet);'
+ ' return false;">'
+ 'Toggle stylesheet<\/a>\n';

document.write(s);

// Use the SGML comment in HTML only -->
</script>

The changeSheet(...) function must set the new stylesheet for the link
accordingly:

function changeSheet(whichSheet)
{
// ...

// convert argument to a number and assign value to global variable
currentSheet = +whichSheet;

// REMOVE THIS, we have zero-based indexes everywhere now
whichSheet=whichSheet-1;

...
// use currentSheet instead
...
}

By passing a reference to a container object (say, the
HTMLAnchorElement object itself) which properties are
modified instead of a primitive value, you can avoid the
`currentSheet' global variable.
HTH

PointedEars
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.