javascript - "e instanceof TouchEvent" in Safari doenst work - Stack Overflow

admin2025-04-18  3

On Safari for MacOs this simple code part isnt working for me at all. e instanceof TouchEvent - it seems that for some reason the "TouchEvent" is not fully supported by Safari for MacOs. It does work on Safari for iOs which is even stranger imo.

ReferenceError: Could not find "TouchEvent" variable.

Is there a proper solution for solving this issue?

On Safari for MacOs this simple code part isnt working for me at all. e instanceof TouchEvent - it seems that for some reason the "TouchEvent" is not fully supported by Safari for MacOs. It does work on Safari for iOs which is even stranger imo.

ReferenceError: Could not find "TouchEvent" variable.

Is there a proper solution for solving this issue?

Share asked Jan 30 at 11:07 David StögmüllerDavid Stögmüller 12 bronze badges 2
  • 3 TouchEvent is not supported in Safari – VLAZ Commented Jan 30 at 11:09
  • "solving this issue" - as in, avoid the JavaScript error? Or as in, will work the exact same way in Safari as in other browsers? For the former, typeof TouchEvent !== "undefined" && e instanceof TouchEvent should probably "work." – C3roe Commented Jan 30 at 13:13
Add a comment  | 

1 Answer 1

Reset to default 0

So i came over this post where i finally found the working solution for me. I think everyone should know this little trick as it can save costs and lots of time. The classic, well-known Apple issues - year after year ...

https://blog.cotten.io/desktop-safari-still-hates-instanceof-touchevent-1fcdda8409d5

The trick is to use: const isTouch = ("touches" in e); instead of const isTouch = e instanceof e;

Which is strange but i think we have to deal with it :D

转载请注明原文地址:http://www.anycun.com/QandA/1744924206a89554.html