472,119 Members | 910 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

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

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
Jul 20 '05 #1
3 17497


bob wrote:
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');
};

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Philip Ronan <ph***********@virgin.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
bob
thanks
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dante | last post: by
2 posts views Thread by Brett Baisley | last post: by
12 posts views Thread by Kepler | last post: by
9 posts views Thread by alu | last post: by
11 posts views Thread by Tony | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.