473,503 Members | 7,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17674


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2023
by: Dante | last post by:
Hello. I have a Javascript that gets data from an XML document and displays it through javascript. The problem is that when I do dcfile.getElementsByTagName("subhead").firstChild.nodeName all I...
2
2364
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
12
10123
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
15
3286
by: lawrence | last post by:
Is this the correct way to test for a method before I use it? createRange() is, I believe, an IE only method. function wrapSelectionInTag(selection, tag) { if (document.selection.createRange)...
9
36708
by: alu | last post by:
Could someone explain the discrepancies within and between the stated definitions / usage of 'document.location' , 'document.URL' vs. their actual functionality? Should they be read-only when in...
11
460
by: Tony | last post by:
Is it me, or is document.write just about the most abused js function? Maybe, like goto, js would be better without it? Is there any good reason to use it? Because I'm having a hard time seeing...
8
7083
by: Mateusz Viste | last post by:
Hi, I am trying make some multimedia files playable from my website. So far, I am able to generate dynamically a new page containing the right <embed> section. However, when I load my script, it...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.