Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 2 characters in body
Source Link
Neo Morina
  • 401
  • 3
  • 14

A really good way of detecting mobile or tablet devices is by looking if the browser can create a touch event.

Plain JavaScript Code:

function isMobile() { try{ document.createEvent("TouchEvent"); return true; } catch(e){ return false; } } if (isMobile()) { # do whatever you wanna do! } 

This worked for me really well, but there may be a problem with laptop devices which include a touchscreen display.

I am not sure if it would detect a touchscreen Laptop will get detected as a mobile device because I haven't tested it yet.

A really good way of detecting mobile or tablet devices is by looking if the browser can create a touch event.

Plain JavaScript Code:

function isMobile() { try{ document.createEvent("TouchEvent"); return true; } catch(e){ return false; } } if (isMobile()) { # do whatever you wanna do! } 

This worked for me really well, but there may be a problem with laptop devices which include a touchscreen display.

I am not sure if it would detect a touchscreen Laptop as a mobile device because I haven't tested it yet.

A really good way of detecting mobile or tablet devices is by looking if the browser can create a touch event.

Plain JavaScript Code:

function isMobile() { try{ document.createEvent("TouchEvent"); return true; } catch(e){ return false; } } if (isMobile()) { # do whatever you wanna do! } 

This worked for me really well, but there may be a problem with laptop devices which include a touchscreen display.

I am not sure if a touchscreen Laptop will get detected as a mobile device because I haven't tested it yet.

Source Link
Neo Morina
  • 401
  • 3
  • 14

A really good way of detecting mobile or tablet devices is by looking if the browser can create a touch event.

Plain JavaScript Code:

function isMobile() { try{ document.createEvent("TouchEvent"); return true; } catch(e){ return false; } } if (isMobile()) { # do whatever you wanna do! } 

This worked for me really well, but there may be a problem with laptop devices which include a touchscreen display.

I am not sure if it would detect a touchscreen Laptop as a mobile device because I haven't tested it yet.