Connecting Tech Pros Worldwide Help | Site Map

Cross browser attach event function and firefox events

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 2nd, 2007, 05:55 AM
acl123
Guest
 
Posts: n/a
Default 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.  

  #2  
Old March 2nd, 2007, 06:05 AM
RobG
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.