469,353 Members | 2,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

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 17251


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
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.