DEV Community

Ryo Kuroyanagi
Ryo Kuroyanagi

Posted on

Detecting visionOS by JavaScript

As of July 2024, you can use the following code to determine if a browser is on visionOS or not. (This does not identify browser brands; I only checked major browsers.)

function isVisionOS () {
  return navigator.userAgent.includes("(Macintosh;") &&
    !!navigator.xr &&
    document.ontouchstart !== undefined;
}
Enter fullscreen mode Exit fullscreen mode

Breakdown

The function is using 3 criteria.

1. Whether the user agent string contains "(Macintosh;" or not.

Safari on macOS, iPadOS, visionOS has user agents like following.

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15
Enter fullscreen mode Exit fullscreen mode

By using the following code, we can determine if the browser is on macOS, iPadOS, or visionOS.

navigator.userAgent.includes("(Macintosh;")
Enter fullscreen mode Exit fullscreen mode

2. Whether the browser supports XR or not.

According to the MDN page, Safari for macOS, iPadOS does not support XR. However, Safari on visionOS supports.

You can check if XR is supported with the following code:

!!navigator.xr
Enter fullscreen mode Exit fullscreen mode

So far, it seems like criteria No.1 and No.2 are enough to identify visionOS. However, Chrome on macOS supports XR. So we need No.3.

3. Whether it is a touch device or not.

Finally, we can exclude macOS by using the following code.

document.ontouchstart !== undefined
Enter fullscreen mode Exit fullscreen mode
OS Browser UserAgent navigator.xr Touch enabled
visionOS (2.0) Safari
visionOS (2.0) Firefox (128.3)
Windows 11 Chrome (127.0.6533.72)
Windows 11 Edge (127.0.2651.74)
Android 12 (Pixel 6) Chrome (126.0.6478.188)
iOS (18.0) Chrome (127.0.6533.77)
iOS (18.0) Safari
iPadOS (17.5.1) Chrome (127.0.6533.77)
iPadOS (17.5.1) Safari
macOS (Sonoma 14.5) Chrome (126.0.6478.114)
macOS (Sonoma 14.5) Safari

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay