Current location: Hot Scripts Forums » General Web Coding » JavaScript » [SOLVED] Handle event listener


[SOLVED] Handle event listener

Reply
  #1 (permalink)  
Old 06-02-08, 01:30 PM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Handle event listener

I write a function to handle javascript "addEventListener" for Firefox and Internet Explorer:

javascript Code:
  1. 1: function addEvent(element, event, eventListener)
  2. 2: {
  3. 3if (typeof(element.addEventListener) != "undefined")
  4. 4{
  5. 5:    element.addEventListener(event, eventListener, false);
  6. 6}
  7. 7else
  8. 8:  {
  9. 9:    var thisListener = function()
  10. 10:   {
  11. 11:   var event = window.event;
  12. 12:   
  13. 13:             if (Function.prototype.call)
  14. 14:   {
  15. 15:    eventListener.call(element, event);
  16. 16:   }
  17. 17:  };
  18. 18:  
  19. 19:  element.attachEvent("on" + event, thisListener);
  20. 20}
  21. 21: }

I get the code between line 9: and 17: from web. I don't know, is a good idea to create a function (object) like this inside addEvent function? Exist a better way to do this?

Thanks
Reply With Quote
  #2 (permalink)  
Old 06-02-08, 01:40 PM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
it looks pretty good to me. If it works, then I wouldn't search for a better way. There is one tricky bit though:

In IE, you can attach multiple eventlisteners to one event, without one overwriting the other. In Firefox, it is possible to only add 1 eventlistener to the event. Good thing about the firefox approach is that if you forget to remove an eventlistener before adding one, then you won't have unexpected results. In IE however, it is possible that multiple functions are triggered.

To circumvent that, you have to find a way to make sure that only 1 eventlistener is triggered for a particular event. (in IE)
__________________
Jack Bauer makes Chuck Norris cry
Reply With Quote
  #3 (permalink)  
Old 06-02-08, 01:41 PM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
thanks, but what "var thisListener = function()" do exactly, create a function or object ?

This is not better: ?

javascript Code:
  1. function addEvent(element, event, eventListener)
  2. {
  3.     if (typeof(element.addEventListener) != "undefined")
  4.     {
  5.         element.addEventListener(event, eventListener, false);
  6.     }
  7.     else
  8.     {
  9.         function thisListener()
  10.         {
  11.             var event = window.event;
  12.            
  13.             if (Function.prototype.call)
  14.             {
  15.                 eventListener.call(element, event);
  16.             }
  17.         }
  18.  
  19. element.attachEvent("on" + event, thisListener);
  20. }

The both method work for me.. But I don't know is a good idea to define a function in another function ?

Last edited by zoliky; 06-02-08 at 01:48 PM.
Reply With Quote
  #4 (permalink)  
Old 06-02-08, 01:44 PM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
A function. It's only an object if you give the function properties.

javascript Code:
  1. var listener = function(){alert("foo");};
  2. alert(typeof listener);
  3.  
  4. function listener2(){
  5. alert("bar");
  6. }
  7. alert(typeof listener2);
__________________
Jack Bauer makes Chuck Norris cry
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Differentiate between mouseup event and click event daivagna JavaScript 3 11-06-07 07:44 AM
Event handling manish.garg Windows .NET Programming 2 09-07-07 02:32 PM
Event Registration/Management bdarcyevans Job Offers & Assistance 1 02-26-07 01:50 AM
Event date database with reminder and... meslemicha Script Requests 0 03-25-05 07:10 PM


All times are GMT -5. The time now is 11:38 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.