Cross browser attach event function and firefox events

A

acl123

Hi,
I am trying to work out how to access the event object in firefox
under two conditions -
1) Attaching event handlers in javascript, not html.
2) The event handler requires parameters in addition to the event
object.

Here is an HTML page that demonstrates the problem. Notice that it
works in IE but not firefox.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">
<html>
<head>
<title>My page</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>

<form action="">

<p><input id="testBox" type="text" /></p>

</form>

<script type="text/javascript">

function XBrowserAddHandler(target,eventName,handlerName)
{
if ( target.addEventListener )
target.addEventListener(eventName, handlerName, false);
else if ( target.attachEvent )
target.attachEvent("on" + eventName, handlerName);
else
target["on" + eventName] = handlerName;
}

var y = 3;

var txtTestBox = document.getElementById('testBox');

XBrowserAddHandler(txtTestBox, 'keyup', function()
{ myFunctionRef(y) });

var myFunctionRef = function myFunction(x, e)
{
var keyCode;

if (!e && window.event)
e = window.event;

if (e)
keyCode = (window.Event) ? e.which : e.keyCode;

alert('You pressed: ' + keyCode + '. Event object is :' + e + '. x
is: ' + x);
}
</script>

</body>
</html>
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top