displayDate()
function is automatically invoked when page loads.But when i call displayDate
function without using parenthesis then it works perfectly.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Click "Try it" to execute the displayDate() function.</p>
<button id="myBtn">Try it</button>
<p id="demo"></p>
<script>
document.getElementById("myBtn").onclick = displayDate();
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
</body>
</html>
This code sample is from w3schools