0

Somewhat of a noob here! What I want to do, by using javascript, is to get a random number between 10 and 25 when it’s summer, -5 and 10 when it’s spring and fall and then -20 and -5 when it’s winter. I then want to connect three different CSS-styles to these three intervals. stylesommar.css when it’s summer, stylevår/höst.css when it’s spring/fall and stylevinter.css when it’s winter. Have been struggling with this for a long time and would be so grateful if anyone of you experts could help me out? This is the code I’ve got so far but it obviously doesn’t work, it just feels like a mess:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">

<script type="text/javascript">




var currentTime = new Date()
var month = currentTime.getMonth() + 1
var total = month;




if (total >=6 && total <= 8)
{
document.write('<img src="bakgrund1.jpg" width="117" height="100">');   
(randomNumber > 10);
}
else if (total >=9 && total <= 11)
{
document.write('<img src= "bakgrund2.jpg"  width="117" height="100"/>');
(randomNumber <= 10 && randomNumber >= -5);
}

else if (total ==12 || total == 1 || total == 2)
{
document.write('<img src= "bakgrund3.jpg" width="117" height="100" />');
(randomNumber < -5);

}

else if (total >=2 && total <= 6)
{
document.write('<img src= "bakgrund4.jpg" width="117" height="100" />');
(randomNumber <= 10 && randomNumber >= -5);

} 
else
{
document.write("<b><br>Error!</b>");
}
</script>




<script>

function myFunction()
{

var randomNumber=Math.floor(Math.random()*50)-20;

if (randomNumber > 10){
$ ('<link href="stylesommar.css" rel="stylesheet" type="text/css">');
}

if (randomNumber <= 10 && randomNumber >= -5){
$ ('<link href="stylevårhöst.css" rel="stylesheet" type="text/css">');
}
if (randomNumber <= -5 && randomNumber >= -20){
$ ('<link href="stylevinter.css" rel="stylesheet" type="text/css">');
}
}
</script>




<script>
var d = new Date()
var timeHour = d.getHours()
var strtime = ""

if (timeHour > 5)
(
strtime = "God morgon!"
)
if (timeHour > 11)
(
strtime = "God eftermiddag!"
)
if (timeHour > 17)
(
strtime = "God kväll!"
)
if (timeHour > 19)
(
strtime = "God natt!"
)
if (timeHour < 6)
(
strtime = "Dags att sova!"
)
document.write(strtime)
</script>




</head>

<script src="jquery-1.8.3.min.js"></script> 

<script>
$(document).ready(function(){
$("a:link").mouseover(function(){
$("a:link").css("background-color","yellow");
 });
 $("a:link").mouseout(function(){
$("a:link").css("background-color","lightgray");
});
});
</script>
<script>
$(document).ready(function(){
 $("#hide").click(function(){
$("#opacitybox").hide();
 });
 $("#show").click(function(){
$("#opacitybox").show();
});
 });
</script>
<script>
 $(document).ready(function(){
$("button").click(function(){
$("#opcitybox").fadeTo("slow",0.15);
$("p").fadeTo("slow",0.4);
});
});



 </script>


</head>
<body>
<div id="layout">
 <div class="rundh">
 <div class="rundtopph">
<div class="rundh_r1"></div>
<div class="rundh_r2"></div>
<div class="rundh_r3"></div>
<div class="rundh_r4"></div>
 </div>
</div>
<div id="sidhuvud">
</div>
<div id="kolumncontainer">
<div id="kolumn1">
 <div id="menycontainer">
<h1>Sida 1</h1>
<ul id="meny">
<li><a href="index.php" id="current">Huvudmeny 1</a></li>
<li><a href="sida2.php">Huvudmeny 2</a></li>
<li><a href="sida3.php">Huvudmeny 3</a></li>
<li><a href="sida4.php">Huvudmeny 4</a></li>
<li><a href="sida5.php">Huvudmeny 5</a></li>
</ul>
</div>
<div id="opacitybox">
<p>F&ouml;rdjupningsuppgiften g&auml;ller jQuery. En plusuppgift ger ett VG och ge ett  MVG.<br>
 A. Skapa en klickbar meny d&auml;r ett antal undermenyer dyker upp n&auml;r ni   klickar p&aring; huvudmenyn. Anv&auml;nd Slide() f&ouml;r det.<br>
 B. Anv&auml;nd n&aring;gon av de jQuery-funktioner som inte n&auml;mndes p&aring;    f&ouml;rel&auml;sningen</p>
<img src="Umu_campus.jpg" width="180" height="110">   
<p> Klicka för att matcha bakgrundsfärg med aktuell temp</p> 
<button onclick="myFunction()">Testa!</button>

</div>
<p> Visa/dölj</p>
<button id="hide">Hide</button>
<button id="show">Show</button>
</div>
<div id="kolumn2">
<h3>Del 1</h3> <button>Klicka för att göra textblock genomskinliga</button>
 <h4>Information</h4>
 <p> Ni ska i detta moment skriva ett JavaScript som p&aring;verkar en webbsidas   utseende.
Bilder ska bytas ut beroende p&aring; datum. F&ouml;rslagsvis byter ni bilder varje   kvartal eller varje m&aring;nad.
F&auml;rgerna ska &aring;terspegla aktuell temperatur. Temperaturen simuleras genom   att man slumpar ut en l&auml;mplig temperatur f&ouml;r aktuell &aring;rstid.       F&ouml;rslagsvis slumpar ni ut ett tal mellan 10 och 25 p&aring; sommaren, mellan -5 och   10 p&aring; h&ouml;st och v&aring;r och mellan -20 och -5 p&aring; vintern. Ni kan anv&auml;nda er av funktionen Math(). Med den g&aring;r det att slumpa fram en temperatur.</p>
<h3>Del 2</h3>
 <h4>Information</h4>
 <p>Vidareutveckla din webbsida s&aring; att du med hj&auml;lp av JavaScript &auml;ndrar p&aring; textinneh&aring;llet n&aring;gonstans i din sida. Exempelvis slumpar du ut n&aring;got visdomsord, eller s&aring; utg&aring;r du fr&aring;n den kod du skrev i moment 1 och ser till s&aring; att du byter ut n&aring;gon text s&aring; att den b&auml;ttre passar till &aring;rstiden.</p>
  <h4>Information</h4>
  <p> H&auml;r ska ni anv&auml;nda er av jQuery f&ouml;r att skapa ett antal effekter, ni ska implementera 3 olika saker:
&#61607; Ni ska anv&auml;nda mouseOver() f&ouml;r att &auml;ndra utseendet p&aring; en l&auml;nk n&auml;r muspekaren st&aring;r &ouml;ver l&auml;nken.
&#61607; Hide() och Show() ska anv&auml;ndas f&ouml;r att visa och d&ouml;lja en div d&auml;r ni har lagt text och bild. Ni kan anv&auml;nda Toggle() om ni vill.
&#61607; fadeTo() ska anv&auml;ndas f&ouml;r att g&ouml;ra tv&aring; textblock genomskinliga. De ska ha olika opacitet s&aring; det beh&ouml;vs tv&aring; funktioner f&ouml;r att l&ouml;sa detta. Metoden fadeTo() anropas p&aring; valfritt s&auml;tt.</p>
  <h4>Information</h4>
  <p>F&ouml;rdjupningsuppgiften g&auml;ller jQuery. En plusuppgift ger ett VG och ger ett MVG.<br>
A. Skapa en klickbar meny d&auml;r ett antal undermenyer dyker upp n&auml;r ni klickar p&aring; huvudmenyn. Anv&auml;nd Slide() f&ouml;r det.<br>
B. Anv&auml;nd n&aring;gon av de jQuery-funktioner som inte n&auml;mndes p&aring; f&ouml;rel&auml;sningen</p>
</div>
</div>
 <div id="sidfot">
</div>
</div>
</body>
</html>
Bergi
  • 630,263
  • 148
  • 957
  • 1,375

2 Answers2

0
$ ('<link href="….css" rel="stylesheet" type="text/css">');

That just creates the element, but does nothing with it. You will need to append it to the DOM to apply the stylesheet:

$ ('<link href="….css" rel="stylesheet" type="text/css">').appendTo("head");
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Ok thanks! One step closer. Now it changes the CSS when I press the "testa" - button but it just changes the CSS randomly, not to the right season. – user2107645 Apr 07 '13 at 12:56
  • Um, yes, your `myFunction` does only create a `randomNumber` (from -20 to 30). It does not use the code to determine the season as you did for the background images. That's why I didn't see how you assignment relates to the code… – Bergi Apr 07 '13 at 13:06
0

I rewrote your code below. The logic in your code was basically on track, but there were some errors that wouldn't allow it to work.

Here, we're using a function called getRandomInt() to generate the random number. It accepts two arguments for the min and max values. Based on the month, we call that function with the min/max that you specified. That number is then passed to myFunction() which writes the correct CSS file to the document.

var currentTime = new Date(),
    month = currentTime.getMonth() + 1;

if (month >=6 && month <= 8) {
  document.write('<img src="bakgrund1.jpg" width="117" height="100">');   
}
else if (month >=9 && month <= 11){
  document.write('<img src= "bakgrund2.jpg"  width="117" height="100"/>');
}
else if (month ==12 || month == 1 || month == 2) {
  document.write('<img src= "bakgrund3.jpg" width="117" height="100" />');
}
else if (month >=2 && month <= 6) {
  document.write('<img src= "bakgrund4.jpg" width="117" height="100" />');
}    

function getMonth() {
  var currentTime = new Date();
  return currentTime.getMonth() + 1; 
}

function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getTemp() {    
  var month = getMonth(),
      randomNumber;
  if (month >=6 && month <= 8) {
    randomNumber = getRandomInt(10, 25);
  }
  else if (month >=9 && month <= 11){
    randomNumber = getRandomInt(-5, 10);
  }
  else if (month == 12 || month == 1 || month == 2) {
    randomNumber = getRandomInt(-20, -5);
  }
  else if (month >= 2 && month <= 6) {
    randomNumber = getRandomInt(-5, 10);
  }     
  return randomNumber;
}

function myFunction() {
  var month = getMonth();
  if (month ==  11 || month == 12 || month == 1 || month == 2) {
    $ ('<link href="stylevinter.css" rel="stylesheet" type="text/css">').appendTo("head");      
  }
  if (month >= 3 && month <= 6) {
    $ ('<link href="stylevårhöst.css" rel="stylesheet" type="text/css">').appendTo("head");      
  }      
  if (month >= 7 && month <= 10) {
    $ ('<link href="stylesommar.css" rel="stylesheet" type="text/css">').appendTo("head");
  }
}

There are more efficient ways to accomplish what you're doing, but my answer shows how to do it with the code you gave.

EDIT: Didn't notice that myFunction() is called onclick the first time. Here is an updated version that uses functions for getting the current month and a random number within that month's range. Let me know if you have any questions.

EDIT2: I'm not sure what the random numbers and ranges are about. I removed them in my answer. Feel free to adjust myFunction() to better reflect the 4 month period that represents each season.

EDIT3: We now have random numbers (temperatures) again. I'm not sure where you're using the temperates, but to get the random temp you just call getTemp()

Source for getRandomInt() is here:

https://stackoverflow.com/a/1527820/1091949

Community
  • 1
  • 1
user1091949
  • 1,933
  • 4
  • 21
  • 27
  • `myFunction` is called from a button onclick, so you can't use `document.write` inside. – Bergi Apr 07 '13 at 13:24
  • Oh you're right, didn't notice that. I'll fix it now. Thanks. – user1091949 Apr 07 '13 at 13:25
  • "user1091949" I think you're on the right track! But "Bergi" what do you mean by the "document.write" - comment? Is the code not accurate? – user2107645 Apr 07 '13 at 13:45
  • He was correct. I didn't noitce that `myFunction()` is executed by clicking a button, not on page load. I updated my answer to fix it. – user1091949 Apr 07 '13 at 13:48
  • I think you're close but the css is still changing randomly? =/ – user2107645 Apr 07 '13 at 13:50
  • Tell me this, what are the random numbers and ranges for? That's kind of throwing things for a loop, it seems. – user1091949 Apr 07 '13 at 13:56
  • The random numbers are supposed to represent the temperature for the current season (Celsius). Like I mentioned earlier it's a school assignment so these elements just needs to be there. – user2107645 Apr 07 '13 at 14:00
  • I deleted the "myfunction - button" and now it works! Thanks so much "user1091949", you're the man! – user2107645 Apr 07 '13 at 14:26