are custom events possible?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cool Mentalist

    are custom events possible?

    I know you can use custom events in other scripts but can u do it in
    javascript? how?
  • Yann-Erwan Perio

    #2
    Re: are custom events possible?

    Cool Mentalist wrote:
    [color=blue]
    > I know you can use custom events in other scripts but can u do it in
    > javascript? how?[/color]


    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">Di spatch a foo event</span>
    <script type="text/javascript">
    var d=document, fs;
    if(d.getElement ById){
    fs=d.getElement ById("foospan") ;
    if(fs && d.createEvent){
    fs.onclick=func tion(evt){
    var e=document.crea teEvent("Events ");
    if(typeof e!="undefined") {
    e.initEvent("fo o", false, false);
    this.dispatchEv ent(e);
    }
    }
    fs.addEventList ener("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 ("createEventOb ject" 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.

    Comment

    Working...