functions without arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Hester

    functions without arguments

    In my neck of the woods functions usually have arguments. For example f(r) = 3 + r is a nice function. f(5) = 8, f(-3) = 0. In JavaScript we could write it like this:

    function f(r){
    return (r + 3);
    }

    easy enough.

    Now say we have a <INPUT element that has the onmouseover event defined like this:

    <INPUT name="test"... onmouseover="Ja vaScript:alert( f(3));".../>

    When the mouse goes over this elemnt you will get a message box that contains the value 6.

    But what if we would like to remove the attribute and assign the onmouseover this way:

    <INPUT name="test".../>

    <script type="javascrip t">
    <!-- Begin
    document.getEle mentById('test' ).onmouseover = f
    function f(r){
    return (r+3);
    }
    // End -->
    </script>

    This won't work. I understand why it doesn't but I don't understand if assigning event handlers in this fashion REQUIRES argumentless functions. Is that right? Thanks.

    --
    George Hester
    _______________ _______________ ____
  • to heave chunks

    #2
    Re: functions without arguments

    ><.script type="javascrip t">[color=blue]
    ><!-- Begin
    >document.getEl ementById('test ').onmouseover = f
    >function f(r){
    > return (r+3);
    >}
    >// End -->
    ><./script>
    >
    >This won't work. I understand why it doesn't but I don't understand if =
    >assigning event handlers in this fashion REQUIRES argumentless =
    >functions. Is that right? Thanks.[/color]

    I don't you think you understand this as well as you may think. When you add
    an event handler attribute to a HTML tag, the scripting engine attaches an
    anonymous function to the event handler.

    For instance,

    <.div onmouseover="al ert( 3);">

    The function that is attached to the onmouseover event handler looks like this.

    function Anonymous() {
    alert( 3);
    }

    You do not attach "alert( 3);" to the onmousover event, you attach an Anonymous
    function which contains "alert( 3);".

    A more accurate representation of what you are trying to accomplish would be
    this.

    function Test_MO_Functio n() {
    alert( f( 3));
    }

    function f( r) {
    return (r+3);
    }

    document.getEle mentById( "elmTest").onmo useover = Test_MO_Functio n;


    Peace, Vm
    Yaz

    Providing complicated solutions to simple problems since 1997.

    Comment

    • George Hester

      #3
      Re: functions without arguments

      _______________ _______________ ____
      "to heave chunks" <justyaz@aol.co mpels.me> wrote in message news:2004012600 0820.18858.0000 0746@mb-m28.aol.com...
      [color=blue]
      > I don't you think you understand this as well as you may think. When you add
      > an event handler attribute to a HTML tag, the scripting engine attaches an
      > anonymous function to the event handler.
      > [/color]
      [color=blue]
      > Peace, Vm
      > Yaz
      > [/color]
      Sure that's why I asked. I am not too ashamed to be dumb. It happens. I understand a function I just didn't understand JavaScript's use of it. Thanks Vm I think I can use your hint.

      Comment

      Working...