473,769 Members | 4,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(wh ichSheet){
whichSheet=whic hSheet-1;
if(document.sty leSheets){
var c=document.styl eSheets.length;
for(var i=0;i<c;i++){
if(i!=whichShee t){
document.styleS heets[i].disabled=true;
}else{
document.styleS heets[i].disabled=false ;
}
}
}
}
Jul 20 '05 #1
2 2869
> 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="Javascrip t:changeSheets( ' + (i + 1) + ')"'
' onclick="change Sheets(' + (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="Javascrip t:changeSheet(! currentSheet)"'
+ ' onclick="change Sheet(!currentS heet);'
+ ' 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(whi chSheet)
{
// ...

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

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

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

By passing a reference to a container object (say, the
HTMLAnchorEleme nt 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
2080
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 response.write to write this way: Dim i As Integer = 1 If i = 1 Then Response.Write("<LINK href=""Stylesone.css"" type=""text/css"" rel=""stylesheet"">") Response.Write("<script language =""javascript"" src = ""testone.js"">
1
1411
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" type="text/css" href="<%=CurrentTheme%>"> because this requires that I declare a public string (CurrentTheme) in every page. I want to do it more transparently, to work with any page that links
3
11920
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 inherit all my pages from it so the css link below is added to each page automatically in the <head> section.... <LINK href="/MyApp/Css/MyApp.css" type="text/css" rel="stylesheet"> Thanks, Dave.
2
2510
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 page. In this code behind's Page_Load event which sets a boolean called useAlternateStyle to true or false based on business logic. Now from my main page (test.aspx) I want to either write out HTML that sets the style sheet to "mainStyle.css"...
2
2086
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 parameters Now I want to use this parameters to build the xpath, but I might get some of the values as null, so in that case I must eliminate the null values. And now the solution for this, is, dynamically building the
2
2168
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 on the logged-in user's saved preferences. The user can define their own styles (font-size, font-family, color, etc.)
4
5903
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 option. Problem is - when you set an ID, it prefixes it with ctl0_ on IIS 5.1 (Windows XP - our dev machines) and ctl00_ on IIS 6.0 (Windows 2003 server - the production machine) The obvious answer is to create two versions of style stylesheet -...
3
8706
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 idea or coding for this purpose. Can anybody help me.Thanks in advance. cheers, raja
6
2559
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.