473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamically changing a stylesheet

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
2 2845
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Amir Eshterayeh | last post by:
Dear Friends I need to change the css file of my aspx files dynamically so users can choose their css file names and when page loads, it read css file name from database. I try to use...
1
by: JezB | last post by:
How can I dynamically change an external stylesheet link (or import) at runtime in server-side code ? I don't want to use a placeholder as in <link id="myTheme" rel="stylesheet"...
3
by: Dave | last post by:
Is there a way to dynamically add a reference to the css stylesheet from the codebehind similarly to the script registration features? I was thinking of adding this code to a base class and...
2
by: Steve Franks | last post by:
What is the proper/best way to dynamically change the HTML written out to the browser based on the value from a variable in a code behind page? For instance, I have test.aspx.vb as a code behind...
2
by: ajayreddy2105 | last post by:
Hi All, I want to build the xpath expression dynamically in the xsl for for-each. Scenario is like this:: 1. From javascript I am sending the parameters to xsl. 2. In xsl I am taking those...
2
by: Jason_SanDiego2006 | last post by:
Hello all, Hang with me, I'm a little new. I'm working on a web application in C# using ASP.NET 2.0 The goal of my application is to have pages whose styles can be dynamically changed based...
4
by: Dunc | last post by:
I've got a c# app, and in the HTML in front I have a <divtag around some navigation controls, and I want to dynamically change it's ID so the CSS kicks in and highlights the currently selected...
3
by: rajachezhian | last post by:
hello, i am new to javascript. i want to use DIV object for expand/collapse the Tree Dynamically like Microsoft explorerer.MY size of the tree elements are changeing Dynamically. I need some...
6
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.