Web Analytics Made Easy -
StatCounter what IS object in onclick routines - CodingForum

Announcement

Collapse
No announcement yet.

what IS object in onclick routines

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • what IS object in onclick routines

    I've tried 2 different javascripts for dong numbers only. Both work fine in Firefox but both get 'object missing' errors in IE.
    ie:
    function numbersonly(myfield, e, dec)

    onKeyPress="return numbersonly(this, event)"


    I'm no pro at javascript I come from the world of 'parameters'
    but dosen't the line represent what goes to 'this' and the keys
    code goes with 'e'? dec is decimal but it seems like it should have a default of 'no' or 0 or whatever the d*** thing is looking for. The other routine gets me into the same error and it doesn't even ask about decimals. OH help and bother!

  • #2
    this and event are reserved keywords. "this" is a stand-in for "the current object" and "event" for the "currently active event object" (which is in inline-JS the event defined by the event attribute, i.e. onKeyPress => keypress-event). it stems from the lack of proper event coding originating in the early versions of JS (this type of event definition is commonly referred to as DOM-0 (= not part of any DOM standard) inline event handling).

    for certain JavaScript actions you need to access either the currently executing event (e.g. to get the value of a pressed key (i.e. "a" if you press the a-key)) and/or the current HTML object (e.g. a text field, in your case it looks like the window object (used e.g. for only allowing number input to a text field)).

    while in the event models other than the inline model "this" is automatically populated (some exceptions for IE) and the event object is passed as first parameter (except in IE), the inline model totally lacks that and thus these values have to be passed manually by means of passing the reserved keywords "this" and "event".

    the main reason (IMO) inline events are still used (despite their lack of advantages) are – outdated tutorials (esp. for beginners)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

    Comment

    Working...
    X