Posted on Feb. 15, 2021, 8 p.m. by Bishal ( 3323)
onload
event. The Document Object Model (DOM) onload
event runs when an object has been loaded. In this Project, We are going to call a function using the onload
event.<body onload="function()"></body>
addEventListener
to check if the object has been loadeddocument.addEventListener("load", function);
index.html
index.html
<!DOCTYPE html>
<html>
<head>
<title>How to get current time - Code With Bishal</title>
</head>
<!-- created by Code With Bishal - www.codewithbishal.com
-->
<body>
</body>
</html>
script.js
<head> </head>
of your HTML document:
<script src="script.js"></script>
script.js
file:
function showTime(){
var date = new Date();
var h = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();
var time = h + ":" + m + ":" + s;
document.getElementById("cwb-time").innerText = time;
document.getElementById("cwb-time").style.color = "#FFCC70";
document.getElementById("cwb-time").style.fontSize = "70px";
setTimeout(showTime, 1000);
}
<!DOCTYPE html>
<html>
<head>
<title>Change colour of Div using click event - Code With Bishal</title>
</head>
<!-- created by Code With Bishal - www.codewithbishal.com
-->
<body onload="showTime()">
<h2 class="cwb-time" id="cwb-time"></h2>
</body>
</html>
function showTime(){
var date = new Date();
var h = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();
var time = h + ":" + m + ":" + s;
document.getElementById("cwb-time").innerText = time;
document.getElementById("cwb-time").style.color = "#FFCC70";
document.getElementById("cwb-time").style.fontSize = "70px";
setTimeout(showTime, 1000);
}