Skip to content

Instantly share code, notes, and snippets.

@Dchole
Last active May 1, 2021 12:16
Show Gist options
  • Save Dchole/3a0ba4dccb427558558ed85b217d671c to your computer and use it in GitHub Desktop.
Save Dchole/3a0ba4dccb427558558ed85b217d671c to your computer and use it in GitHub Desktop.
Script to alert user what device they are using in raw vanilla JS
const BUTTON = document.querySelector("button");
const { userAgent } = window.navigator;
// Set device to "mobile" if "Mobi" exists in userAgent else set device to "desktop"
const device = userAgent.includes("Mobi") ? "mobile 📱" : "desktop 💻";
BUTTON.addEventListener("click", () => alert(`You are on ${device}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment