/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Javascript selection of time-specific css files

I get the feeling that this place isn’t really for asking entire codes from, but if somebody has the freetime I’d be very thankful if they would help.

I’ve been working a lot with html, css, and the stuff in between for nearly a year now, and as all I have delt with is the personal website I use them for, I never put much investment into javascript, so anything beyond the basics is like a foreign language to me.

What I’m trying to do in my latest design is, depending on the time of the day, to load different css files so that I can have the colors dark and alluring at night, bright in the morning, so on and so forth. I have seen cheap codes change text according to the time of the day using javascript, but changing the css file seems like something entirely different.

What I am hoping for is some javascript code that will allow the css file to change between something generic such as sheet1.css, sheet2.css, and sheet3.css, such that

Sheet1.css loads from 4am-11:59pm
Sheet2.css loads from 12pm-7:59pm
Sheet3.css loads from 8pm-3:59m

I figured the one minute before hand so as not to cause problems, unless the whole less than stuff does the job, than the times could just be rounded.

[only as a side note, it would be nice to know how, when a link is clicked, to load that file temporarily (I don’t want to mess with cookies), so if I clicked on my own specified link that said ‘morning’ it would load sheet1.css for that page]

so if somebody has some free time, or knows the code right from mind, I’d really appreciate it if you could show it to me ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Bog_ZaicevJun 24.2006 — [CODE]
<script>
var wow = new Date();
q=Math.ceil(((wow.getHours()+20)%24+1)/8)
document.write('<LINK href="'+q+'.css" type=text/css rel=stylesheet>');
</script>[/CODE]
Copy linkTweet thisAlerts:
@mcfrostyauthorJun 24.2006 — =/ hmm. it doesn't want to select any css file. I have an external javascript link in the head of the page as well, so I played around with removing that and putting your code alone and then in the external file as well, and nothing worked either. is that made to write the css link in the header?
Copy linkTweet thisAlerts:
@LogicianJun 24.2006 — =/ hmm. it doesn't want to select any css file. I have an external javascript link in the head of the page as well, so I played around with removing that and putting your code alone and then in the external file as well, and nothing worked either. is that made to write the css link in the header?[/QUOTE]That code as written requires you to have files called 1.css, 2.css & 3.css

To keep your current names, you could try this code in the head. Just substitute the file names (add a relative path to all of them if they are not in the same folder).
[CODE]<script type='text/javascript'>

var styles=['early.css','afternoon.css','late.css'], hour=new Date().getHours();

styleIndex=(hour > 11 && hour < 20) ? 1 : (hour > 3 && hour < 12) ? 0 : 2;

document.write("<link rel='stylesheet' href='" +styles[styleIndex]+"'>");

</script>
<noscript>
// your default HTML link tag here
</noscript>
[/CODE]
Copy linkTweet thisAlerts:
@mcfrostyauthorJun 24.2006 — nice!! thanks, you guys are the best!
×

Success!

Help @mcfrosty spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...