Forum Moderators: open

Message Too Old, No Replies

CSS and Javascript

Change CSS based on link

         

treeleaf20

2:26 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



I want to change change the style sheet for each page based on a selection by a user. The problem is that I need to change the whole style sheet for the page not just certain elements on the page. I tried to use this code but it doesn't like the eval() function. Please help if you can:
<script type="text/javascript">
function switchstyles(style)
{
//<link rel="STYLESHEET" id="default" type="text/css" href="http://www.test.com/includes/style.css">
if (style == 'HC')
{
alert('Test');
}
if (style == 'Default')
{
alert('TestDefault');
}else{
alert('Call Function');
eval("\<link rel='STYLESHEET' type='text\/css' href='http:\/\/www.test.com\/health\/includes\/style_hc.css'\>");
//<link rel="STYLESHEET" type="text/css" href="http://www.test.com/includes/style.css">
}
}
</script>

The reason for this is everytime a link is clicked I want to pass the value of the link to the switchsytles() and have it load that stylesheet instead of the default one. Thanks for all the help in advance!

Rambo Tribble

3:29 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A style sheet is loaded as a part of a document loading; without reloading the document it is hard to imagine how you would get an entirely new style sheet to load. As this thread illuminates, changing style objects is a risky business, while changing the attributes of an element is relatively reliable: [webmasterworld.com...]

Bernard Marx

3:36 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's because you are asking eval to evaluate a string of HTML code, which it (quite rightly) doesn't consider meaningful Javascript.

TIP #1: Never use eval. There is always something better.
(When you know of the 1 or 2 exceptions, you won't be needing my advise anyway. Just don't use it.)

You probably meant document.write(), but that would overwrite the page.
In fact, switching stylsheets is easier.
[tested IE]

<html>
<head>
<title>Stylesheet Swap</title>
<link rel = "stylesheet" href = "sheet1.css" type = "text/css">
<link rel = "stylesheet" href = "sheet2.css" type = "text/css" disabled="true">
<link rel = "stylesheet" href = "sheet3.css" type = "text/css" disabled="true">
<!-- keep script block, or link below stylesheets -->
<script language="JavaScript">

var sheets = document.styleSheets
var iCurrSheet = 0

function toggleSheets(index)
{
sheets[iCurrSheet].disabled = true
sheets[index].disabled = false
iCurrSheet = index
}

</script>
</head>
<BODY>
<div style="width:100px;">demo div</div>
<!-- void(0) prevents link being followed -->
<a href="javascript:toggleSheets(0);void(0)">Sheet 0 ¦ div{background-color:red;}</a></br>
<a href="javascript:toggleSheets(1);void(0)">Sheet 1 ¦ div{background-color:green;}</a></br>
<a href="javascript:toggleSheets(2);void(0)">Sheet 2 ¦ div{background-color:blue;}</a>

</body>
</html>

birdbrain

3:37 pm on Jun 30, 2004 (gmt 0)



Hi there treeleaf20,

This will change the style sheet...

 
<html>
<head>

<link id="default" rel="stylesheet" type="text/css" href="http://www.test.com/includes/style.css">

<script type="text/javascript">
<!--
function testStyles() {
if(document.getElementById('default').href=='http://www.test.com/includes/style.css') {
alert('TestDefault');
}
else {
if(document.getElementById('default').href=='http://www.test.com/health/includes/style_hc.css') {
alert('Test');
}
}
}
function switchStyles(url) {
document.getElementById('default').href=url;
alert(document.getElementById('default').href);
}
onload=testStyles;
//-->
</script>

</head>
<body>

<div>
<a href="#" onclick="testStyles();switchStyles('http://www.test.com/health/includes/style_hc.css')">style_hc.css</a><br />
<a href="#" onclick="testStyles();switchStyles('http://www.test.com/includes/style.css')">style.css</a>
</div>

</body>
</html>

birdbrain

Bernard Marx

4:16 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So now we have 2 methods. With ups & downs (mainly all ups). In IE that is.
Internet Explorer will happily accept the changing of href and disabled properties, and will reference a link by id, whether W3C likes it or not (do they?).

My current Mozilla doesn't recognise the 'disabled' attribute, and any attempt to change a linked sheet's href results in:

Error: setting a property that has only a getter

This is commonly done. So I'd better go have a peek somewhere.

treeleaf20

5:38 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



The code that you made worked good and I appreciate it greatly but I was wondering if it can be done without using getElementById because it has to be comptabile with NS4+. Also since disabled doesn't work with mozilla can anyone think of an idea to help combat that problem also. It would be great to work with all browsers. Thanks for all the help again if this can be done.

treeleaf20

8:37 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



Everything is looking good and I thank everyone a lot. I have one more quick question. I also have a background on my site and I'm not sure how to assign an ID to a background. It seems to me that you have to do something different as it won't update when the next sytle sheets are implemented....does anyone know what you have to do differently to get the background of the site to change as well?

Bernard Marx

9:28 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Set the BG with your stylesheet(s)
body
{
background-color:#aabbcc;
background-image:url(image.jpg);
}

treeleaf20

1:13 pm on Jul 1, 2004 (gmt 0)

10+ Year Member



The only problem with that is I'm trying to put it in a <TD> tag and when I try and use:

TD.back
{
background-color:#000000;
background-image:url(http://www.test.com/images/sprigs_search.jpg);
}

and then call it like this:
<td class="back" width="175" height="95" bgcolor="#99CCCC">

It won't display anything. Anyone know what change I might have to make to get it work properly inside the <td> instead of just making it a background for the whole page?

treeleaf20

1:45 pm on Jul 1, 2004 (gmt 0)

10+ Year Member



When I do it like this though it works:

<td style="BACKGROUND-IMAGE: url(http://www.test.com/stuff/images/sprigs_search.jpg);" width="175" height="95">

I can't have it like this since I'm changing the style sheets though so I need it to work from the stylesheets but I figured I'd give some more ideas that I've already tried!

Bernard Marx

2:28 pm on Jul 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Re: #9

The BG color spec in the CSS is being overridden by the (old-timey) bgcolor attribute in the tag. That said, I can't account for the BG image not appearing (this should appear 'over' the BG colour).

Remove bgcolor/background tag attributes, and see what happens from then on.

Maybe best to take this to the CSS forum too, where you'll find someone who knows their stuff.