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

Discussion in 'Javascript' started by bob, Aug 21, 2003.

  1. bob

    bob Guest

    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
     
    bob, Aug 21, 2003
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Aug 21, 2003
    #2
    1. Advertising

  3. Philip Ronan <> 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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Aug 21, 2003
    #3
  4. bob

    bob Guest

    thanks
     
    bob, Aug 21, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sasha
    Replies:
    3
    Views:
    429
    Scott Allen
    Mar 2, 2005
  2. Eduardo Schettino

    [ANN] DoIt 0.1.0 Released (build tool)

    Eduardo Schettino, Apr 19, 2008, in forum: Python
    Replies:
    8
    Views:
    357
    Eduardo Schettino
    Apr 23, 2008
  3. Eduardo Schettino

    ANN: doit 0.2.0 released

    Eduardo Schettino, Apr 17, 2009, in forum: Python
    Replies:
    0
    Views:
    261
    Eduardo Schettino
    Apr 17, 2009
  4. schettino72
    Replies:
    0
    Views:
    311
    schettino72
    Sep 1, 2009
  5. Eduardo Schettino

    [ANN] doit 0.5

    Eduardo Schettino, Dec 2, 2009, in forum: Python
    Replies:
    0
    Views:
    250
    Eduardo Schettino
    Dec 2, 2009
Loading...

Share This Page