0

I'm looking to load different css files according to the date (season).

I tried modifying an image script from here on stackoverflow, but that didn't work.

Can anybody point me out where it goes wrong?

<link href="/css_season/default.css" rel="stylesheet" type="text/css" onload="logo(this)">
function logo(link) {
  var d = new Date();
  var Today = d.getDate();
  var Month = d.getMonth();
  var src;
  if (Month === 4 && (Today >= 1 && Today <= 30)) {
    src = "/css_season/easter.css";
  } else if (Month === 7 && (Today >= 1 && Today <= 31)) {
    src = "/css_season/vacation.css";
  } else if ((Month === 8 && Today >= 30) || (Month === 0 && Today <= 2)) {
    src = "/css_season/vacation.css";
  } else if (Month === 12 && (Today >= 15 && Today <= 31)) {
    src = "/css_season/holidays.css";
  } 
  link.href=href;
}
1

2 Answers 2

1

Your assignment link.href=href won't work because href isn't defined. Also, I would put the logo function in <body onload="logo();"> and give your link tag an id attribute.

This should work for you:

<html>
<head>
<link id="cssId" href="/css_season/default.css" rel="stylesheet" type="text/css"/>
</head>
<body onload="logo();">
 <!-- body content here -->
</body>

function logo() {
  var d = new Date();
  var Today = d.getDate();
  var Month = d.getMonth();
  var src;
  if (Month === 4 && (Today >= 1 && Today <= 30))
    src = "/css_season/easter.css"; 
  else if (Month === 7 && (Today >= 1 && Today <= 31))
    src = "/css_season/vacation.css";
  else if ((Month === 8 && Today >= 30) || (Month === 0 && Today <= 2))
    src = "/css_season/vacation.css";
  else if (Month === 12 && (Today >= 15 && Today <= 31))
    src = "/css_season/holidays.css";

  document.getElementById('cssId').href=src;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Another argument for placing onload on the body instead of the link: many browsers don't support onload on link (yet). Still, there's room for improvement: you could stop using the onload attribute entirely and attach a load event handler inside the script itself (separating HTML from JavaScript); also you could place all styles in one CSS file and let JavaScript place a class on the body (instead of changing the loaded CSS) to distinguish between styles.
Thank you so much for the quick and accurate response ! I will check out and respond.
Just to be sure, I probably can add <script type="text/javascript"> and </script> around the javascript?
This <script> and </script> around the Javascript will do.
I tried the script, but there seems to be an error, nothing happens. Could it be conflicting with other jquery on the page? What is the structure to load several jquery best to have no conflicts?
1

You have wrong assignment at the end of the js function.
link.href=href; should be link.href = src;

Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.