开发者

Is it considered a good practice to create and dispatch custom Javascript events?

My web application uses document.createEvent and event.initEvent to create custom events of the generic type Event.

I wonder whether this is considered good practice. On the hand side, this way I can make use of the DOM event system that is already there and do not have to invent and implement my own; on the other hand this may lead to name clashes in case future standardised event models define an event type with the name I have chosen. (Or is it possible to namespace event types?)

I am asking because I have just learnt by browsing stackoverflow that putting custom properties on DOM objects or builtin Javascript objects is considered bad practise.

EDIT I think I have found something: http://dev.w3.org/2006/webapi/DOM-Level-3-Events/html/DOM3-Events.html#events-CustomEvent

So let me rephrase my question: Am I understanding the quoted section correctly in that it is advised to actually use a CustomEvent inside a web application?

ADDED WARNING I have just noticed tha开发者_开发技巧t CustomEvent is not supported in Firefox versions before 6. It is supported in current Webkit-based browsers, though.


If you're going to create something and make it as namespace-proof as you can, you can do what Webkit and Mozilla do and add a name-moniker to the beginning of all of your event names. Like:

_marc_trap_door_shut
_marc_trap_door_open
_marc_trap_door_ajar
... etc ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜