event confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rick Pasotto

    #1

    event confusion

    >From what I've read when this is clicked on:

    <a href="#" onclick="DoIt() ">Some Text</a>

    a function defined as:

    function DoIt(e) {
    if (e) alert("got an event");
    }

    should work in Mozilla. For IE I'd have to use e=window.event first.

    However it doesn't work for me. What *does* work is:

    <a href="#" onclick="DoIt(e vent)">Some Text</a>

    The word "event" has to be there. This *doesn't* work:

    <a href="#" onclick="DoIt(e vt)">Some Text</a>

    Is that right? Have I misread the docs?

    Here's a simple test program: <http://niof.net/tjs.htm>

    --
    "The whole aim of practical politics is to keep the populace alarmed --
    and hence clamorous to be led to safety--by menacing it with an endless
    series of hobgoblins, all of them imaginary." --- H. L. Mencken
    Rick Pasotto rick@niof.net http://www.niof.net
  • Lee

    #2
    Re: event confusion

    Rick Pasotto said:[color=blue]
    >[color=green]
    >>From what I've read when this is clicked on:[/color]
    >
    ><a href="#" onclick="DoIt() ">Some Text</a>
    >
    >a function defined as:
    >
    >function DoIt(e) {
    > if (e) alert("got an event");
    >}
    >
    >should work in Mozilla. For IE I'd have to use e=window.event first.
    >
    >However it doesn't work for me. What *does* work is:
    >
    ><a href="#" onclick="DoIt(e vent)">Some Text</a>[/color]


    Yes, that is what the documentation says is required.
    A parameter named "event" is passed to the event handler.
    If you want to be able to see it in your DoIt() function
    (which is NOT the event handler function), you need to
    pass it as a parameter.

    Comment

    • Michael Winter

      #3
      Re: event confusion

      On Thu, 14 Oct 2004 23:19:10 -0500, Rick Pasotto
      <rickpasotto@ch i.news.speakeas y.net> wrote:
      [color=blue]
      > From what I've read when this is clicked on:
      >
      > <a href="#" onclick="DoIt() ">Some Text</a>
      >
      > a function defined as:
      >
      > function DoIt(e) {
      > if (e) alert("got an event");
      > }
      >
      > should work in Mozilla. [...][/color]

      No, it won't. You haven't passed any arguments to DoIt(), so e will be
      undefined in every browser.
      [color=blue]
      > However it doesn't work for me. What *does* work is:
      >
      > <a href="#" onclick="DoIt(e vent)">Some Text</a>
      >
      > The word "event" has to be there. This *doesn't* work:
      >
      > <a href="#" onclick="DoIt(e vt)">Some Text</a>
      >
      > Is that right? Have I misread the docs?[/color]

      When you add an intrinsic event as an attribute to a HTML element, the
      browser creates an anonymous function that contains your code. In Mozilla
      (and some other browsers), this anonymous function has an argument, event,
      whilst in IE there is no argument. Consider:

      <input ... onclick="myFunc tion(event);">

      Mozilla creates, in effect, this:

      inputElement.on click = function(event) {
      myFunction(even t);
      };

      and IE, this:

      inputElement.on click = function() {
      myFunction(even t);
      };

      In the first case, it's obvious that myFunction is using the argument
      provided by the browser. In the second case, the browser resolves event to
      be the global event object.

      Hope that helps,
      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      Working...