By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

are custom events possible?

P: n/a
I know you can use custom events in other scripts but can u do it in
javascript? how?
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Cool Mentalist wrote:
I know you can use custom events in other scripts but can u do it in
javascript? how?

Well, the DOM Events specification does provide some ways to create and
dispatch events, be it regular or custom events. This is working at
least in Mozilla:

<span id="foospan">Dispatch a foo event</span>
<script type="text/javascript">
var d=document, fs;
if(d.getElementById){
fs=d.getElementById("foospan");
if(fs && d.createEvent){
fs.onclick=function(evt){
var e=document.createEvent("Events");
if(typeof e!="undefined"){
e.initEvent("foo", false, false);
this.dispatchEvent(e);
}
}
fs.addEventListener("foo", function(evt){
alert("Hello I'm an event of type "+evt.type);
}, false);
}
}
</script>
The specification can be found at
<URL: http://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/>

Unfortunately, the specification isn't really supported across browsers.
IE permits to create and dispatch "regular" events using proprietary
methods ("createEventObject" and "fireEvent"), but that's pretty much it
I believe.

As for creating events in pure javascript, this isn't possible, you'd
need to implement them yourself with functions/behaviors.
HTH
Yep.
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.