+ 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/#
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();
}
+ 8
@james
check the update
nomeh's solution works in external js
+ 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
+ 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;
}
+ 5
thanks guys, but isn't there way to implement it from the js page also?
+ 5
why did it not work when i created a time function and set window.onload = time() ?
+ 5
Right on, I got it now man. Thanks for all the answers. They were all insightful.
cheers!
+ 4
kk thanks againđ
+ 4
JavaScript code placement (where you place your code in the DOM) is as important as your back account. :) good one Nomeh