472,146 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Cross browser attach event function and firefox events

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.  
Mar 2 '07 #1
1 15551
On Mar 2, 4:39 pm, "acl123" <andrewclawre...@gmail.comwrote:
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

Mar 2 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John Fereira | last post: by
1 post views Thread by cjl | last post: by
2 posts views Thread by Eric Caron | last post: by
7 posts views Thread by dwandless | last post: by
1 post views Thread by Rancid Buttchutney | last post: by
7 posts views Thread by =?iso-8859-1?q?Jesper_R=F8nn-Jensen?= | last post: by
2 posts views Thread by mattgarvin | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.