document.createEvent fails

18,311

Solution 1

From the documentation:

type is a string that represents the type of event to be created. Possible event types include "UIEvents", "MouseEvents", "MutationEvents", and "HTMLEvents".

So you probably want:

var e = document.createEvent('HTMLEvents');
e.initEvent('TestEvent', true, true);

See event.initEvent.

Update: Maybe document.createEvent('Event'); is even better for custom events, but it is part of DOM Level 3 and I don't know how much supported it is.

Solution 2

Use one of these event types: https://developer.mozilla.org/en/DOM/document.createEvent#Notes

TestEvent is not a supported event type. Better use "MouseEvents" or "HTMLEvents".

Solution 3

There's simply no event type called TestEvent:

https://developer.mozilla.org/en/DOM/document.createEvent#section_4
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-DocumentEvent-createEvent

Maybe you meant TextEvent?

PS: Next time do at least a bit of own research before using SO like you would use Google ;)

Share:
18,311
shmichael
Author by

shmichael

Updated on July 29, 2022

Comments

  • shmichael
    shmichael almost 2 years

    The following code fails (in javascript console, and also when injecting a script via browser extension)

    document.createEvent('TestEvent')
    

    Firebug spits out:

    [Exception... "Operation is not supported" 
     code: "9" 
     nsresult: "0x80530009 (NS_ERROR_DOM_NOT_SUPPORTED_ERR)" 
     location: "http://www.google.com 
     Line: 71"] 
    

    Chrome gives a similar error message. What am I doing wrong?

  • shmichael
    shmichael over 13 years
    I'm trying to create a custom DOM event. Guess I have to do more reading.