In the following code, once I run it, it waits for click event to fire an alert. However if I change the following javascript code
from
clickMeButton.onclick=runTheExample;
to
clickMeButton.onclick=runTheExample();
it always fires up an alert when page downloaded without any click event. I would like to know what is the difference. I am using Chrome. Snipped code is as follows:
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
<script type="text/javascript" src="script13.js"> </script>
</head>
<body>
<h1 id="title">DOM Example </h1>
<p id="first">This is the first paragraph</p>
<p id="second"><strong>This is the second paragraph</strong></p>
<p id="third">This is the third paragraph</p>
<input type ="text" id="myTextBox" />
<input type ="submit" id="clickMe" value="Click Me!"/>
<a href="http://www.google.com" id="MyGoogle"> Google! </a>
</body>
</html>
//script13.js
window.onload= function(){
var clickMeButton=document.getElementById('clickMe');
clickMeButton.onclick=runTheExample;
}
function runTheExample(){
alert('running the example');
}