Connecting Tech Pros Worldwide Forums | Help | Site Map

Cross browser attach event function and firefox events

acl123
Guest
 
Posts: n/a
#1: Mar 2 '07
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
  3. TR/html4/strict.dtd">
  4. <html>
  5. <head>
  6. <title>My page</title>
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. </head>
  9. <body>
  10.  
  11. <form action="">
  12.  
  13. <p><input id="testBox" type="text" /></p>
  14.  
  15. </form>
  16.  
  17. <script type="text/javascript">
  18.  
  19. function XBrowserAddHandler(target,eventName,handlerName)
  20. {
  21. if ( target.addEventListener )
  22. target.addEventListener(eventName, handlerName, false);
  23. else if ( target.attachEvent )
  24. target.attachEvent("on" + eventName, handlerName);
  25. else
  26. target["on" + eventName] = handlerName;
  27. }
  28.  
  29. var y = 3;
  30.  
  31. var txtTestBox = document.getElementById('testBox');
  32.  
  33. XBrowserAddHandler(txtTestBox, 'keyup', function()
  34. { myFunctionRef(y) });
  35.  
  36. var myFunctionRef = function myFunction(x, e)
  37. {
  38. var keyCode;
  39.  
  40. if (!e && window.event)
  41. e = window.event;
  42.  
  43. if (e)
  44. keyCode = (window.Event) ? e.which : e.keyCode;
  45.  
  46. alert('You pressed: ' + keyCode + '. Event object is :' + e + '. x
  47. is: ' + x);
  48. }
  49. </script>
  50.  
  51. </body>
  52. </html>
  53.  
  54.  

RobG
Guest
 
Posts: n/a
#2: Mar 2 '07

re: Cross browser attach event function and firefox events


On Mar 2, 4:39 pm, "acl123" <andrewclawre...@gmail.comwrote:
Quote:
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.
Some reading:

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


--
Rob

Closed Thread


Similar JavaScript / Ajax / DHTML bytes