+ 3

Javascript help

My Javascript is throwing up a error, I have search online without success. Can you help me? https://code.sololearn.com/W6NWgzPx7AjI/#

16th Aug 2017, 8:04 AM
James Barrow
James Barrow - avatar
9 Answers
+ 6
@James, sure you can but make sure to call the time() function inside window.onload here //This is the first code from JavaScript&JQuery by Jon Ducket. However, I have made some changes to the code to accomidate the invironment /* if you want a time() function create one and make sure that you call it inside window.onload */ function time(){ var today = new Date(); var hourNow = today.getHours(); var el = document.getElementById("Greeting"); if (hourNow > 18){ el.innerHTML = "Good Evening"; } else if (hourNow > 12){ el.innerHTML = "Good Afternoon"; } else if (hourNow > 0){ el.innerHTML = "Good Morning"; } } /* now call the time() function inside window.onload */ window.onload = function(){ time(); }
16th Aug 2017, 9:15 AM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 8
@james check the update nomeh's solution works in external js
16th Aug 2017, 8:43 AM
Lord Krishna
Lord Krishna - avatar
+ 7
include the script in body(solo puts it in head which loads 1st and causes issues) note: footer comes after the body element... https://code.sololearn.com/WSrqy6lDiAxN/?ref=app
16th Aug 2017, 8:36 AM
Lord Krishna
Lord Krishna - avatar
+ 5
here... //This is the first code from JavaScript&JQuery by Jon Ducket. However, I have made some changes to the code to accomidate the invironment window.onload = function(){ var today = new Date(); var hourNow = today.getHours(); var el = document.getElementById("Greeting"); if (hourNow > 18){ el.innerHTML = "Good Evening"; } else if (hourNow > 12){ el.innerHTML = "Good Afternoon"; } else if (hourNow > 0){ el.innerHTML = "Good Morning"; } } ...on the CSS code, remember to make h1 { color: #fff; }
16th Aug 2017, 8:39 AM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 5
thanks guys, but isn't there way to implement it from the js page also?
16th Aug 2017, 8:41 AM
James Barrow
James Barrow - avatar
+ 5
why did it not work when i created a time function and set window.onload = time() ?
16th Aug 2017, 8:59 AM
James Barrow
James Barrow - avatar
+ 5
Right on, I got it now man. Thanks for all the answers. They were all insightful. cheers!
16th Aug 2017, 9:19 AM
James Barrow
James Barrow - avatar
+ 4
kk thanks again😉
16th Aug 2017, 8:46 AM
James Barrow
James Barrow - avatar
+ 4
JavaScript code placement (where you place your code in the DOM) is as important as your back account. :) good one Nomeh
16th Aug 2017, 8:50 AM
Elvis Ngboki
Elvis Ngboki - avatar