Using document.createEvent("TouchEvent")
will not work on desktop-devices. So you can use it inside an if statement.
Note that this method could produce errors on non-touch devices. I would prefer checking for ontouchstart
in document.documentElement
.