开发者

document.createEvent fails

开发者 https://www.devze.com 2023-02-03 15:28 出处:网络
The following code fails (in JavaScript console, and开发者_如何学JAVA also when injecting a script via browser extension)

The following code fails (in JavaScript console, and开发者_如何学JAVA 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?


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消