View Full Version : accessing to event in a function

05-28-2005, 03:25 PM
i have some html:

<div id='mydiv' onmouseover='sth('ff',this);'>blah</div>

how can i access the event object in a function, without changing parameters?

in IE, i can use simply event, but in others(mozilla)?

and is any other way to see the object, who fired an event, without giving 'this' in a parameter of sth()?

05-28-2005, 05:15 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


<script language="JavaScript" type="text/javascript">
function Event(evt){
var e=evt||window.event;
if(!e) return;
if(e.type){ this.type = e.type; alert(e.type); }
if(e.target) {this.target = e.target; alert(e.target.tagName);}
else if(e.srcElement){ this.target = e.srcElement; alert(e.srcElement.tagName); }



<span onclick="Event(event);" >document.Show.Show1.value=vic;</span>


05-28-2005, 08:50 PM
no, no, no.

i DONT wanna give event through parameters of function.

05-30-2005, 05:45 AM
i DONT wanna give event through parameters of function.You have to, to make it work with Moz browsers.

But if you assign the event programmatically, you don't have to pass the event object as it is passed internally.

<div id="mydiv">blah</div>
<script type="text/javascript">
function myFunc(evt){
if (!evt) evt = event;

document.getElementById("mydiv").onmouseover = myFunc;

But if you hardcode the handler, you have to pass the event.

<div id="mydiv" onmouseover="myFunc(event)">blah</div>

05-30-2005, 05:54 AM
function sth(blabla) {
var event = window.event || arguments.callee.caller.arguments[0];

// do stuff with event now

You don't need to pass in the event object explicitly through an argument. Mozilla and the like generate an internal function to represent the event handler, which is passed the event object and calls the actual javascript in the attribute value. So arguments.callee.caller accesses this internal function, and arguments[0] grabs the event object.

06-05-2005, 08:28 PM
i just love posts like that, thanks:)