2

How would you test if an event is a browser event (e.g. click, load, blur etc) based on its name (e.g. click)? (and no I'm not using jQuery/mootools/prototype/dojo)

UPDATE:

In this function I attach browser events (click and load) for example:

observe: function(elements, eventName, callback) { if (!(elements.length != null)) { elements = [elements]; } return this.each(elements, function(e) { if (helper.hooks.eventObserved != null) { helper.hooks.eventObserved(eventName, callback, e); } if (typeof e === 'string') { e = this.el(e); } if (e.addEventListener) { return e.addEventListener(eventName, callback, false); } else if (e.attachEvent) { return e.attachEvent("on" + eventName, callback); } }); }, 

And this fires the event:

fire: function(elements, eventName) { if (!(elements.length != null)) { elements = [elements]; } return this.each(elements, function(e) { var evt; if (document.createEventObject != null) { evt = document.createEventObject(); return e.fireEvent("on" + eventName); } else { evt = document.createEvent('HTMLEvents'); evt.initEvent(eventName, true, true); return !e.dispatchEvent(evt); } }); }, 

But what I want is to test if the event exists for example click is a browser event but login isn't so how would I test that?

4
  • 3
    If you're not using jquery/mootools/prototype/dojo, then where would the events be coming from if they're not "browser events"? Commented Apr 17, 2011 at 21:04
  • (Note that I'm resisting the temptation to guess that your a Qooxdoo user.) Commented Apr 17, 2011 at 21:04
  • I'm writing my own small JS library Commented Apr 17, 2011 at 21:05
  • I don't understand this question. What do you mean by a "browser" event? Where are these events coming from? Can you give an example in code of a problem you want to overcome? Commented Apr 17, 2011 at 21:10

2 Answers 2

1

Check out this solution by Juriy Zaytsev (live demo).

jQuery itself uses it.

Sign up to request clarification or add additional context in comments.

Comments

0

You could check to see if the function/object element["on"+event] exists:

<a href="http://www.google.com" id="myAnchor">test</a> <script> function doesElementHaveEvent(element, event){ return (typeof element["on"+event] != typeof undefined); } alert(doesElementHaveEvent(document.getElementById("myAnchor"), "click")); // true alert(doesElementHaveEvent(document.getElementById("myAnchor"), "login")); // false </script> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.