...

View Full Version : accessing to event in a function



looka
05-28-2005, 02: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()?

vwphillips
05-28-2005, 04:15 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<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); }
}

//-->
</script>
</head>

<body>

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

</html>

looka
05-28-2005, 07:50 PM
no, no, no.

i DONT wanna give event through parameters of function.

glenngv
05-30-2005, 04: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){
alert(evt);
if (!evt) evt = event;
alert(evt);
}

document.getElementById("mydiv").onmouseover = myFunc;
</script>

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

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

jkd
05-30-2005, 04: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.

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum