user7376137
user7376137

Reputation:

How do I make a photo change between the days of the week?

I have a lot of code that I made so far but I can't get the load() function to work:

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Page resizer -->
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<!-- Date -->
<script>
var d = new Date();
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
document.getElementById("date").innerHTML = days[d.getDay()];
</script>

<!-- Gif changer -->
<script>
function changegif() {
  var gif;
  if (days = Sunday) {
    gif = "/img/sunday.gif";
  } else if (days = Monday) {
    gif = "/img/monday.gif";
  } else if (days = Tuesday) {
    gif = "/img/tuesday.gif";
  } else if (days = Wednesday) {
    gif = "/img/wednesday.gif";
  } else if (days = Thursday) {
    gif = "/img/thursday.gif";
  } else if (days = Friday) {
    gif = "/img/friday.gif";
  } else if (days = Saturday) {
    gif = "/img/saturday.gif";
  }
}

function load() {
  document.getElementById('gif').src = changegif();
}
</script>
</head>
<body onload="load()">

<!-- More date stuff -->
<style>
.image { 
   position: relative; 
   width: 100%; /* for IE 6 */
}

h2 { 
   position: absolute; 
   top: 150px; 
   left: 0; 
   width: 100%; 
}

h2 span { 
   color: white; 
   font: bold 24px/45px Helvetica, Sans-Serif; 
   letter-spacing: -1px;  
   background: rgb(0, 0, 0); /* fallback color */
   background: rgba(0, 0, 0, 0.7);
   padding: 10px; 
}

h1 { 
   position: absolute; 
   top: 200px; 
   left: 0; 
   width: 100%; 
}

h1 span { 
   color: white; 
   font: bold 24px/45px Helvetica, Sans-Serif; 
   letter-spacing: -1px;  
   background: rgb(0, 0, 0); /* fallback color */
   background: rgba(0, 0, 0, 0.7);
   padding: 10px; 
}
</style>
<div class="image">
<img src="#" id="gif">
<h2><span>Enjoy Your</span></h2>
<br>
<h1 id="date"><span></span></h1>
</div>

</body>
</html>

If anybody knows what I'm doing wrong please tell me. I have tried integrating the functions into a button but it still didn't work.

Upvotes: 0

Views: 83

Answers (2)

Sebastian Simon
Sebastian Simon

Reputation: 19515

To explain all the mistakes:

  • = assigns; it does not compare.
  • changegif doesn’t return anything, so document.getElementById('gif').src = changegif(); will always set it to undefined (assuming no other errors).
  • Sunday and others are undeclared variable names; they only work as strings: "Sunday".
  • days is not a single day, it’s the whole array, so comparing it to a single day won’t work.

changegif can be simplified to:

function changegif(){
  return "/img/"
    + days[d.getDay()].toLowerCase()
    + ".gif";
}

Or use the answer by Chris G.

Upvotes: 0

user5734311
user5734311

Reputation:

Remove all your own <script>s, then use this:

function load() {
    var day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][new Date().getDay()];
    document.getElementById("date").innerHTML = day;
    document.getElementById('gif').src = "/img/" + day.toLowerCase() + ".gif";
}

Upvotes: 3

Related Questions