Detect if is mobile with Javascript, read user agent
How to detect if your user is using a mobile device in Javascript? You can do it by checking the…
How to detect if your user is using a mobile device in Javascript? You can do it by checking the user agent, that is the “signature” of the browser. When a browser request a page it introduces himself by telling which browser is. This signature is accessible by php on the server, but also by Javascript on the client.
Read User Agent in Javascript:
function isMobile () {
if( navigator.userAgent.match(/iphone/i) ||
navigator.userAgent.match(/ipad/i) ||
navigator.userAgent.match(/android/i) ||
navigator.userAgent.match(/blackberry/i) ||
navigator.userAgent.match(/android/i)
) {
return true;
}
return false;
}
This code search the user agent string for matching for keyword “ipad”, “iphone”, “blackberry” and “android”. If there is an occurence return true, else return false;
