I've been teaching myself Javascript for HTML, over the past week. Mainly from w3schools. I'm trying to make two functions - one to run on the body / window onload event, then this will trigger the update function, which will repeat indefinitely, every 10 / 100 steps.
I'm basically making a website that will be heavily controlled by Javascript. I need to create this template, to use for the system I plan to build (Responsive layout + content, with screen resize).
The initial startup function is not affecting any of the divs. Why is this?
<!DOCTYPE html>
<html>
<head>
<script>
// STARTUP
function startupClass(classnm)
{
// scan through all divs, altering size accordingly.
classid = document.getElementsByClassName(classnm);
for(i=0;i<classid.length;i+=1)
{
// apply style to each div class
classid[i].style.position="absolute";
classid[i].style.width=(screen.availWidth/classid.length);
classid[i].style.height="300px";
classid[i].style.top="0px";
classid[i].style.left=i*(screen.availWidth/classid.length);
classid[i].style.backgroundColor="#f3f3f3";
}
setTimeout(updateClass(classnm),1);
}
// jump to update function (Which then automatically updates, every step)
// UPDATE FUNCTION
function updateClass(classnm);
{
setTimeout(updateClass(classnm),10);
}
window.onload = startupClass("newclass");
</script>
</head>
<body onload="startupClass('newclass');">
<div class="newclass"></div>
<div class="newclass"></div>
<div class="newclass"></div>
<div class="newclass"></div>
</body>
</html>
I'm probably making an obvious mistake, but neither the window.onload, or the body tag onload are triggering the function?
Please forgive me if it's an obvious mistake - I'm very new to Javascript.
ANSWER:
There were a few errors with the syntax - firstly the accidental placement of the semicolon after the updateClass(); {} - then there was an error with the setTimeout() syntax - which was 'setTimeout(updateClass(classnm),1);', changed to 'setTimeout(updateClass,1,classnm);'.
The console no longer threw back any errors - but the divs still weren't being styled. This is because the startupClass() function was running before the divs were placed! In order to fix this, I simply made a very basic function, that then triggered the startupClass function after 1 step:
<head>
<script>
//--------------STARTUP
function startupClassInit(classnm)
{
setTimeout(startupClass,1,classnm);
}
function startupClass(classnm)
{
// scan through all divs, altering size accordingly.
classid = document.getElementsByClassName(classnm);
for(i=0;i<classid.length;i+=1)
{
// apply style to each div class
classid[i].style.position="absolute";
classid[i].style.width=(screen.availWidth/classid.length)+"px";
classid[i].style.height="300px";
classid[i].style.top="0px";
classid[i].style.left=i*(screen.availWidth/classid.length)+"px";
classid[i].style.backgroundColor="#AA0000";
}
// jump to update function (Which then automatically updates, every step)
setTimeout(updateClass,1,classnm);
}
//--------------UPDATE FUNCTION
function updateClass(classnm)
{
classid = document.getElementsByClassName(classnm);
for(i=0;i<classid.length;i+=1)
{
}
setTimeout(updateClass,100,classnm);
}
window.onload = startupClassInit("newclass");
</script>
</head>
This now works fine :) Thank you all for the suggestions and help.