document.onclick=doIt() same as document.onclick=doIt ?

B

bob

Hi,

consider this script:
<script>
function doIt()
{
alert("ok")
}
document.onclick=doIt // or document.onclick=doIt()
</script>

Generally, does it make a difference when there is no parameter to pass?
And when there is a parameter to pass?

Thanks
bob
 
M

Martin Honnen

bob said:
Hi,

consider this script:
<script>
function doIt()
{
alert("ok")
}
document.onclick=doIt // or document.onclick=doIt()
</script>

Generally, does it make a difference when there is no parameter to pass?
And when there is a parameter to pass?

This has nothing to do with parameter passing. If you have
document.onclick = doIt;
then you assign the function doIt to the onclick property. However if
you have
document.onclick = doIt();
then you call the function doIt and assign the result of the function
call to the onclick property of the document object. The result of
calling doIt in your example is the value undefined, thus you assign the
value undefined to the onclick property. Therefore what you want is
document.onclick = doIt;
If you want to pass parameters to doIt then you need to use
document.onclick = function (evt) {
doIt('arg');
};
 
L

Lasse Reichstein Nielsen

Philip Ronan said:
However you could say document.onclick="doIt()". This is a string which will
be evaluated whenever the document.onclick event occurs.

Are you sure this works.
this works:
document.onclick=function(){alert('foo');};
but this doesn't:
document.onclick="alert('foo')";

(tested in IE6)
/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top