Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In javascript 'If mobile phone'

I was thinking of doing something with the jQuery.browser but this only returns which browser you're in and if its a webkit etc etc.

So i basically want to turn off certain js files from even loading if you're on a mobile device?

I assume you can do it but how?

like image 480
Jamie Hutber Avatar asked Jan 28 '12 20:01

Jamie Hutber


People also ask

How do you check if a user is on a mobile device JavaScript?

To detect if the user is using a mobile device in JavaScript, we can use the userAgent property. This property is part of the navigator object and sent by the browser in HTTP headers. It contains information about the name, version, and platform of the browser.

What is the best way to detect a mobile device in JavaScript?

matchMedia() The Window. matchMedia() is one of the best properties for detecting mobile users with JavaScript.

How can I check my mobile device?

Go to the Settings or Options menu, scroll to the bottom of the list, and check 'About phone', 'About device' or similar. The device name and model number should be listed.


1 Answers

I think this answer is better because it doesn't depends on screen width:

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
    // some code..
}

I know that now you've got a brand browser dependency, but this is a bit more maintainable that checking for the screen size.

like image 182
Nahuel Barrios Avatar answered Nov 03 '22 19:11

Nahuel Barrios