22 replies [Last post]
greendog
Offline
Regular
Maybrook, NY
Last seen: 10 years 50 weeks ago
Maybrook, NY
Timezone: GMT-5
Joined: 2007-01-12
Posts: 13
Points: 10

I have just built a site for a store owner, and he wants to restyle the site for each holiday. So I wanted to find out is there a way to switch the external CSS file based on the date?

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

Well, are you using any form

Well, are you using any form of dynamic scripting for the site. If so, it's easy.

greendog
Offline
Regular
Maybrook, NY
Last seen: 10 years 50 weeks ago
Maybrook, NY
Timezone: GMT-5
Joined: 2007-01-12
Posts: 13
Points: 10

Here's the site:

Here's the site: www.pipeline-bagpipes.com

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

Yeah, so, what is it?? PHP?

Yeah, so, what is it?? PHP? ASP.NET? I would have guessed ASP.NET MVC due to the file extensions but then there are query strings instead of routing.

greendog
Offline
Regular
Maybrook, NY
Last seen: 10 years 50 weeks ago
Maybrook, NY
Timezone: GMT-5
Joined: 2007-01-12
Posts: 13
Points: 10

Just plain ol' XHTML,css and

Just plain ol' XHTML,css and a small amount of JavaScript. A little php in the forms.

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 6 years 1 week ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Verschwindende wrote: Well,

Verschwindende wrote:

Well, are you using any form of dynamic scripting for the site. If so, it's easy.

How does posting a link to the site answer the question asked Puzzled

This is a technical forum respond to the questions then solutions can be provided, but don't expect us to have to answer our own questions please.

Edit/ I see you have responded with a little detail.

To quote " A little PHP for forms" that then means your pages are written or can use PHP there is no 'Little' about things your scripting language in use is PHP.

I would now ask this question on a scripting forum dealing with PHP, as easy as this is to achieve using date functions you will need it explained in depth if not written for you; you could also check out script repositories such as HotScripts which may have a pre written function you could use.

You could also follow this chaps guide to doing this it's primarily to change based of time of day but that just needs to be substituted for a month or whatever and is mentioned towards the bottom of the page.

http://www.jek2k.com/wp/2007/05/03/timed-based-css-stylesheet-with-php/

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

greendog
Offline
Regular
Maybrook, NY
Last seen: 10 years 50 weeks ago
Maybrook, NY
Timezone: GMT-5
Joined: 2007-01-12
Posts: 13
Points: 10

My original question was "is

My original question was "is there a way to switch the external CSS file based on the date?"

I didn't, to my knowledge, say "lecture me in the process in posting a simple question on a forum". How are forums effective to users that may, or may not have the same technical knowledge as your own, if someone is going to be chastised for asking a question. Stupid as it may be to you or your peers, it's a valid question. I provided a link, because I figured if Verschwindende looked at the site, he could easily take a look, and see what's involved, rather than going into a long winded description. I'm not a back-end developer, I'm a designer that is very well versed in the practice of CSS. I don't know JavaScript, or even that much PHP. But what I do know is that it was used.

Hugo, I haven't checked out the link yet, so I will, thanks for posting it.

I know now that if I want to ask a simple question that the csscreator.com forum is NOT the place to ask it.

-= Bill =-

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

Wow, lolz. Do the new guys

Wow, lolz. Do the new guys always abuse the regulars here? Just wondering because I didn't get to abuse anyone yet. Who should I have a go at? Tongue

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 6 years 1 week ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Quite often it seems, it's a

Quite often it seems, it's a natural reaction I guess *shrug*

Versh I think you did indeed miss the opportunity but do feel free to catch up, I can take all the abuse you care to throw my way, I was famously called a 'pompous paranoid little tit' I survived it Smile well had a little sob in the corner but pulled myself together quickly Sad(

Greendog do not get too upset you were asked a question you responded with a link that did not answer the question it's not about YOU asking a question we asked the question you failed to answer Smile, this happens more often than you might imagine and is frustrating for us, never ever expect us to do the work, generally you will find few that actually want to find out for themselves what language you are using, we expect you to know that and simply provide the info, or if you are not sure then simply state that fact it also helps us immensely to know the level of expertise of the poster.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

greendog
Offline
Regular
Maybrook, NY
Last seen: 10 years 50 weeks ago
Maybrook, NY
Timezone: GMT-5
Joined: 2007-01-12
Posts: 13
Points: 10

Look, if you can't help me

Look, if you can't help me answer my question, that's fine. But you're getting bent out of shape over the fact that I didn't answer a question. A question, that whether or not it becomes answered, will not make any difference.
Someone could have answered "yes you can use PHP" or "... Javascript" or whatever! That would have been the end of it. I would have even been fine with "Yes" or "No".
If I were walking through the hallway at your job, bumped into you and had the same conversation, I would still say "Here check this site out" and show you and we can have a discussion based upon it.
Besides all of this, we're getting nowhere, and I thank you all for nothing. Now I'm going to ask this question somewhere else, where someone won't have a stupid argument with me.

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

Hugo wrote: ... I was

Hugo wrote:

... I was famously called a 'pompous paranoid little tit' I survived it Smile ...

Aw, they shouldn't have called you paranoid.

Big smile

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

greendog wrote: ... Someone

greendog wrote:

... Someone could have answered "yes you can use PHP" or "... Javascript" or whatever! That would have been the end of it. I would have even been fine with "Yes" or "No". ...

Sorry, man. I thought that was covered.

Verschwindende wrote:

Well, are you using any form of dynamic scripting for the site. If so, it's easy.

slaughters
Offline
newbie
Last seen: 11 years 7 weeks ago
Timezone: GMT-6
Joined: 2009-12-01
Posts: 4
Points: 7

greendog wrote: I have just

greendog wrote:

I have just built a site for a store owner, and he wants to restyle the site for each holiday. So I wanted to find out is there a way to switch the external CSS file based on the date?

I hit this site doing a Google search on the same topic and could not believe no one bothered to answer your question. So I thought I would post an answer here in case anyone else ever stumbles across this like I did.

The answer is "yes", but not with just CSS. You can use JavaScript (it makes no difference what server side code you used. PHP, ASP.NET, JSP, or whatever).

Essentially you get the current date and use that value to decide what external CSS file you want to assign as the new href value of your link tag. Code below shows you what I mean:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="default.css" id="siteCSS"/>
    <script type="text/javascript">
 
        // Get current month
        var currDate=new Date();
        var currMonth = currDate.getDate();
 
        // Month returned by getDate() is 0 to 11 so add 1
        currMonth = parseInt(currMonth) + 1;
 
        // Based on month assign style sheet name
        var newCSS = "default.css";
        if (currMonth >= 1) newCSS = "winter.css"; // Month is January or greater
        if (currMonth >= 3) newCSS = 'spring.css'; // Month is March or greater
        if (currMonth >= 6) newCSS = 'summer.css'; // Month is June or greater
        if (currMonth >= 9) newCSS = 'fall.css';   // Month is September or greater
        if (currMonth = 12) newCSS = 'winter.css'; // Month is December
 
        // Assign new style sheet name as the new href for "siteCSS" LINK tag
        document.getElementById('siteCSS').href = newCSS;
 
    </script>
 
</head>
<body>
 
</body>
</html>

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

I'll be much easier and

I'll be much easier and cleaner just using server-side scripting.

slaughters
Offline
newbie
Last seen: 11 years 7 weeks ago
Timezone: GMT-6
Joined: 2009-12-01
Posts: 4
Points: 7

Please post a server side

Please post a server side example for comparison.

The concept is pretty simple and really could have been boiled down to one core line of javascript. So I will be surprised if a server side example ends up being "easier" and "cleaner".

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

Surprise!

Surprise! Party

int doy = DateTime.Now.DayOfYear - Convert.ToInt32((DateTime.IsLeapYear(DateTime.Now.Year)) && DateTime.Now.DayOfYear > 59);
string currentSeason = String.Format("{0}.css",((doy < 80 || doy >= 355) ? "winter" : ((doy >= 80 && doy < 172) ? "spring" : ((doy >= 172 && doy < 266) ? "summer" : "fall"))));

It works with javascript off, doesn't clutter up the <head> and is also more accurate.

Post edited to replace '+' with '-'

slaughters
Offline
newbie
Last seen: 11 years 7 weeks ago
Timezone: GMT-6
Joined: 2009-12-01
Posts: 4
Points: 7

I can see that you embedded

I can see that you embedded like 3 or 4 ternary operators to assign a character string to a variable based on a date. Did you actually use that value for the pages style sheet yet?

I think you should re-work it a little to use a month number instead of the day number. Not too many people know what date day #175 falls into and another benefit would be that with just 12 numbers you could set up an array to hold the css file names and do away with the harder to read embedded ternary statements.

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

Whoops. I had the leap year

Edit: Thanks for fixing the code snippet above, Hugo.

Whoops. I had the leap year check backwards. :oops:

int doy = DateTime.Now.DayOfYear - Convert.ToInt32((DateTime.IsLeapYear(DateTime.Now.Year)) && DateTime.Now.DayOfYear > 59);
string currentSeason = String.Format("{0}.css",((doy < 80 || doy >= 355) ? "winter" : ((doy >= 80 && doy < 172) ? "spring" : ((doy >= 172 && doy < 266) ? "summer" : "fall"))));

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

slaughters wrote: I can see

slaughters wrote:

I can see that you embedded like 3 or 4 ternary operators to assign a character string to a variable based on a date. Did you actually use that value for the pages style sheet yet?

I don't know what you're asking.

slaughters wrote:

I think you should re-work it a little to use a month number instead of the day number. Not too many people know what date day #175 falls into and another benefit would be that with just 12 numbers you could set up an array to hold the css file names and do away with the harder to read embedded ternary statements.

I was going for accuracy, not ease. I guess I wasted 20 minutes proving a point. Tongue Blah, it was just a quickie. I'm sure I could clean it up a bit besides I like my ternary operators.

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 6 years 1 week ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

slaughters wrote: no one

slaughters wrote:

no one bothered to answer your question.

Thanks for posting the code and helping out. The quote above is not strictly true though is it? Unless you consider that answering a thread of this type means providing a code writing service, which is not really what the forum is about.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

Hugo wrote:slaughters

Hugo wrote:
slaughters wrote:

no one bothered to answer your question.

Thanks for posting the code and helping out. The quote above is not strictly true though is it? Unless you consider that answering a thread of this type means providing a code writing service, which is not really what the forum is about.

Yes, I'm almost certain that I answered the question 4 times beginning with the first reply.

slaughters
Offline
newbie
Last seen: 11 years 7 weeks ago
Timezone: GMT-6
Joined: 2009-12-01
Posts: 4
Points: 7

Hugo - you are correct - you

Hugo - you are correct - you did post a link pointing to a PHP solution. I did not see the link buried at the end of your 2009-11-19 02:23 post.

Verschwindende - Here is the full text of the 5 posts you made (in the order you made them) to the greendogs question. Where did you ever answer him?

1 - Well, are you using any form of dynamic scripting for the site. If so, it's easy.

2 - Yeah, so, what is it?? PHP? ASP.NET? I would have guessed ASP.NET MVC due to the file extensions but then there are query strings instead of routing.

3 - Wow, lolz. Do the new guys always abuse the regulars here? Just wondering because I didn't get to abuse anyone yet. Who should I have a go at?

4 - Aw, they shouldn't have called you paranoid.

5 - Sorry, man. I thought that was covered.

P.S.

Thank you for making my first post to this forum such an horrible experience. I posted to help and was treated like I was stupid to do so. This is seriously the last time I'll bother with this forum.

Verschwindende
Verschwindende's picture
Offline
Guru
Last seen: 1 week 16 hours ago
Timezone: GMT-5
Joined: 2009-10-09
Posts: 2044
Points: 2270

slaughters wrote:...

slaughters wrote:

... Verschwindende - Here is the full text of the 5 posts you made (in the order you made them) to the greendogs question. Where did you ever answer him?

Here's the first answer. I answered "Yes" to his question of is it possible.

Verschwindende wrote:

Well, are you using any form of dynamic scripting for the site. If so, it's easy.

Here's where he said all he needed is a "Yes".

greendog wrote:

... Someone could have answered "yes you can use PHP" or "... Javascript" or whatever! That would have been the end of it. I would have even been fine with "Yes" or "No". ...

This is my second answer telling him his question was already answered and quoting said answer.

Verschwindende wrote:

Sorry, man. I thought that was covered.

Then I posted that it would be better rendered server side.
Then I posted two code snippets proving that server side is better than client side in this case.

I count 4.

slaughters wrote:

Thank you for making my first post to this forum such an horrible experience. I posted to help and was treated like I was stupid to do so. This is seriously the last time I'll bother with this forum.

I thought better and deleted my really hilarious remarks down here. They were pretty good, too. Pity. I'm only messing around now because it's such a huge deal. I find it very funny.

Anyway, I'm sorry that you feel stupid, that has nothing to do with anything I said, but you are the one that challenged my statement so I felt it was correct behavior to prove my point. In all honesty I couldn't possibly care less if someone uses javascript to change their styling as long as the site doesn't rely on it then it's fine. I just think there is a better way and I only thought it proper to express that.

This isn't my place so I'm going to cool it and apologize to the board if I've made a mess of things.