access 'event' in a handler from in-line registration

V

VAXman-

I have code which is created from a content mamagement system. It places
some handlers on tags with in-line registration:

<tag... onmousedown="mousedownHandler(this);" ...>


I want to modified the handler and access the event. Here's an example of
such (very contrived example):

fucntion mousedownHandler(obj) {
if (!e) e = window.event;
this.style.top = e.clientY +'px';
}

This works in Safari as it knows the event as e and I can get it for M$IE
with the window.event. How do I go about accessing it in Firefox?


BTW, I can't readily modify the CMS nor does the customer want to change
CMS.
 
R

RobG

I have code which is created from a content mamagement system. It places
some handlers on tags with in-line registration:

<tag... onmousedown="mousedownHandler(this);" ...>

I want to modified the handler and access the event. Here's an example of
such (very contrived example):

fucntion mousedownHandler(obj) {
if (!e) e = window.event;

As e is (most probably[1]) undefined, the test is always true. A
global variable e is created and its value set as a reference to the
result of evaluating the expression "window.event".

In the IE event model, an event object is available as a property of
the window object. Safari partially supports this model, hence in the
above code e is given a reference to the event object.

Firefox follows the Netscape model for event handling where the event
object must be passed to the function, it isn't available as a global
property.
this.style.top = e.clientY +'px';

}

This works in Safari as it knows the event as e

No, it doesn't. See above.
and I can get it for M$IE
with the window.event.

Which is how Safari gets it too.

How do I go about accessing it in Firefox?

You must pass a reference to the event object when calling the
function.

BTW, I can't readily modify the CMS nor does the customer want to change
CMS.

Then, in polite terms, you're screwed. :)

One solution is to call the handler using:

<... onmousedown="mousedownHandler(event);" ...>

Then you can get the element using the event object:

function mousedownHandler(evt) {
var tgt = evt.target || evt.srcElement;

// In some browsers tgt may be a #text node
if (tgt.nodeType == 3) tgt = tgt.parentNode;
...
}

But that requires modification of the CMS. You might try modifying
the inline handlers to pass the event object as an additional last
argument:

<... onmousedown="mousedownHandler(this, event);" ...>

and use:

function mousedownHandler(obj, evt) {

// evt is the event object in both models
// obj is the element that has the handler

}

So existing functions aren't affected, they'll just ignore the extra
parameter.

You can read about events here:

<URL: http://www.quirksmode.org/js/introevents.html >

1. If you have created a global e somewhere else, e will be defined,
the test will evaluate to false and you won't get the current event
object.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top