Document.createEvent not working

The following code does not work (in the javascript console, and also when entering a script through a 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 displays a similar error message. What am I doing wrong?

+6
javascript javascript-events
source share
3 answers

In the documentation:

type is a string representing the type of event being generated. Possible event types include UIEvents, MouseEvents, MutationEvents, and HTMLEvents.

So you probably want to:

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

See event.initEvent .

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

+16
source share

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

TestEvent not an event type supported. Better to use " MouseEvents " or " HTMLEvents ".

+1
source share

There is no type of event 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

Did you mean TextEvent ?

PS: Next time do at least a little of your own research before using SO, as if you were using Google;)

+1
source share

All Articles