Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.createEvent fails

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?

like image 621
shmichael Avatar asked Jan 10 '11 10:01

shmichael


3 Answers

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.

like image 81
Felix Kling Avatar answered Oct 19 '22 11:10

Felix Kling


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".

like image 28
powtac Avatar answered Oct 19 '22 09:10

powtac


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 ;)

like image 1
Ivo Wetzel Avatar answered Oct 19 '22 11:10

Ivo Wetzel