Is there any way to create TouchEvents or something that works as one in Firefox?



I’m trying to write a browser add-on that can create artificial TouchEvents. So far I have it working in Chrome but Firefox does not seem to support TouchEvents, as trying to create one with new TouchEvent(...) produces:

ReferenceError: TouchEvent is not defined

I’m wondering if there’s some way I can either implement TouchEvent myself in the add-on or otherwise dispatch an event that has all the necessary properties to be usable like a TouchEvent.

However if I create a CustomEvent and assign it the extra properties necessary for it to effectively be a TouchEvent (i.e. touches, targetTouches, and changedTouches) those properties don’t persist by the time the webpage receives the event.

Is there any way to accomplish what I’m after, or am I out of luck trying to get this to work in Firefox?


In case anyone else has this question, here is the solution I found. You can create your own class that extends UIEvent like so:

class TouchEvent extends UIEvent {
    constructor(name, initDict) {
        super(name, initDict);
        this.touches = initDict.touches;
        this.targetTouches = initDict.targetTouches;
        this.changedTouches = initDict.changedTouches;

The one caveat is that these events have to be constructed by a script running in the same domain as the handler that will receive them, otherwise you’ll get a Permission denied to access property error. My solution for that was to use my extension’s content script to add a script element in the current document’s body and set its textContent property to my javascript code that creates and dispatches the touch events.

Answered By – Bri Bri

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More