4 replies [Last post]
HellsBells
HellsBells's picture
Offline
Leader
Bedford, UK
Last seen: 12 years 30 weeks ago
Bedford, UK
Joined: 2004-04-07
Posts: 851
Points: 0

I'm hoping you can help me with this problem - but I'm not sure it's possible. I can manage fairly complicated CSS but I'm not good with Javascript yet - I tend to just cut and paste which can lead to trouble...

I've got a site with a styleswitcher on it (from the Alistapart site) which works nicely - you click on one of two images to swap between "night time" and "day time" stylesheets.

Is it possible to also have a script which will automatically switch between stylesheets at a certain time of day?

I saw a message in this forum a while back with a script for swapping the stylesheets according to time which I grabbed and put into my page, but I'm not convinced that it will work along side the manual switcher - what do you reckon?

This is what it looks like:

<link rel="stylesheet" type="text/css" media="screen" title="default" href="lbpstyle.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="night" href="nightstyle.css" />
<script type="text/javascript" src="styleswitcher.js"></script>
<script type="text/javascript" language="javascript"> 
// <![CDATA[ 
function style() 
   { 
var now = new Date (); 
var hour = now.getHours(); 
if (hour >= 21 && hour <= 6) 
   { 
   document.write('<link rel="stylesheet" type="text/css" media="screen" title="default" href="lbpstyle.css">'); 
   } 
else 
   { 
   document.write('<link rel="alternate stylesheet" type="text/css" media="screen" title="night" href="nightstyle.css">'); 
   } 
   } 
// ]]> 
</script> 
 <script language="javascript">style();</script>

If whoever wrote the code can own up then I can also stick in a credit - I didn't make a note when I took it last time.

Thanks

HellsBells

My strategy is so simple an idiot could have devised it!

"Also, your CSS (no offence) makes me want to gouge my eyes out with a rusty spoon" - TPH

co2
co2's picture
Offline
Leader
UK
Last seen: 12 years 33 weeks ago
UK
Joined: 2003-09-17
Posts: 721
Points: 0

Styleswitcher

Think that was me! Was my first javascript (and currently my last! Tongue )... I must admit thought, although most of it is my code, the bare bones idea of it are from a book tutorial.

The next sentence is true. The previous sentence is false. Discuss...

tommuir
Offline
Regular
Upper east side
Last seen: 13 years 50 weeks ago
Upper east side
Joined: 2004-04-05
Posts: 40
Points: 0

Styleswitcher

Why is everybody using document.write()? It's evil I tells ya! Just Dynamicly change the href attribute of the link tag. (need to make it browser friendlt too...)

i am not here

HellsBells
HellsBells's picture
Offline
Leader
Bedford, UK
Last seen: 12 years 30 weeks ago
Bedford, UK
Joined: 2004-04-07
Posts: 851
Points: 0

Styleswitcher

Thanks co2 - if I use the code I'll include a credit .

Not sure what to do at the moment as I think that I can't include both the manual styleswitcher and the automatic one - if this is the case then I have to choose between having the 'switches' on the page for people to change themselves or having it change automatically at night...

Also - having problems getting the code through validation - the validator doesn't seem to like the phrase 'lanuage' (as in lanuage="javascript" ) and says there's no such term.

I like to think I'm fairly good at CSS but I only use Javascript very occasionally and tend to forget what I've learned so I have to start over reading the huge Javascript book.....then I go watch TV instead.

HellsBells

My strategy is so simple an idiot could have devised it!

"Also, your CSS (no offence) makes me want to gouge my eyes out with a rusty spoon" - TPH

tommuir
Offline
Regular
Upper east side
Last seen: 13 years 50 weeks ago
Upper east side
Joined: 2004-04-05
Posts: 40
Points: 0

Styleswitcher

It should work. You just have to know which should have presidence.
(Where is CO2's code?)

i am not here