Skip to content

Instantly share code, notes, and snippets.

@estevan-ulian
Created May 6, 2025 19:58
Show Gist options
  • Select an option

  • Save estevan-ulian/8f1286c76722f755df6cc55180110e5f to your computer and use it in GitHub Desktop.

Select an option

Save estevan-ulian/8f1286c76722f755df6cc55180110e5f to your computer and use it in GitHub Desktop.
Verify type of userAgent
function getDeviceType() {
const ua = navigator.userAgent.toLowerCase();
const tablet = /ipad|android(?!.*mobile)|tablet|playbook|silk/;
const mobile = /iphone|ipod|android.*mobile|blackberry|bb10|opera mini|windows phone/;
if (tablet.test(ua)) {
return "tablet";
}
if (mobile.test(ua)) {
return "mobile";
}
if (window.matchMedia && window.matchMedia("(pointer: coarse) and (max-width: 1024px)").matches) {
return "tablet";
}
return "desktop";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment