d--b
d--b

Reputation: 5779

Why does event instanceof TouchEvent throw an Exception in Firefox

I am trying to run some code that works on chrome through firefox, but I get an exception.

document.onmousedown = function(e) { 
    try {
        if (e instanceof TouchEvent) {
            alert('haa');
        } 
    }
    catch (ex) {
        alert('hoo');
    }
};

What should I use instead? the TouchEvent is in the mozilla documentation...

jsFiddle here

Upvotes: 4

Views: 1426

Answers (1)

evilpie
evilpie

Reputation: 2941

Firefox doesn't support TouchEvent. However you can MouseEvent.mozInputSource to figure out where the mousedown came from. There are a few different constants defined on MDN.

I can't actually test this, but I think something like this would work:

document.addEventListener("mousedown", function(e) { 
    if ("mozInputSource" in e) {
        var source = e.mozInputSource;
        if (source == MouseEvent.MOZ_SOURCE_PEN ||
            source == MouseEvent.MOZ_SOURCE_TOUCH) {
            alert("probably generated by touch");
        }
    }
});

Upvotes: 2

Related Questions