On Sep 2, 1:05*am, Arun <a.regin...@gmail.comwrote:
[...]
Remember, window.event only works on Internet Explorer versions 5.0
onwards. What you need is a simple cross-browser solution like the
following:
*function foo(e)
*{
* *var sourceElm = (e.target) ? e.target : window.event.srcElement;
* *alert(sourceElm.id);
*}
Given your code below, e is a reference to window.event in IE
already. The usual pattern here is:
function foo(e) {
var e = e || window.event;
var tgt = e.target || e.srcElement;
/* do stuff with e and tgt */
}
Noting that tgt will be reference to the element that initiated the
event, which is not necessarily the element that has the listener
(consider a bubbling event from a span inside the A element).
[...]
..where e is passed as an object of window.event in IE, but in
Firefox, it is passed as an implicit event object. IE uses the
window.event.srcElement routine to get to the calling object,
but in EOMB (every other modern browser), e.target is used to
retrieve the calling object.
You will call the above method simply like this:
* <a id="bar" href="javascript:void(0);" onclick="foo(event)">Click
* *me</a>
Put something meaningful in the href attribute or replace the A
element with something more appropriate (a button or styled span
maybe).
The onclick listener should return false if the link should not be
followed:
<a href="usefulLink.html" onclick="return foo(event);"... </a>
and within foo:
function foo(e) {
if (...) {
/* all OK, don't follow the link */
return false;
} else {
...
}
}
The use of event as an argument to the call to foo means that both in
common event models a reference to the event object will be passed to
foo. It is the only way to get a reference to the event object from
an inline listener in browsers that don't implement window.event.
In any case, as suggested by Thomas, the simplest solution is to have
the listener pass a reference to the element using the this keyword:
<a ... onclick="return foo(this);"... </a>
and:
function foo(el) {
/* el is a reference to the node that called foo */
}
So el will be a reference to the element that has the listener,
regardless of the source or target element.
--
Rob