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.