Detecting Mobile Devices With Jquery

Author : Code With Mark

I’m going to share with you a website that I’ve been using in my projects lately. It’s called apimk.com and on this website they will allow you to make a few api calls and in return they will give you some key information.

For example, I don’t if this ever happened to you or not before, but one time I was working on a project and I wanted to know whether my user was accessing the site from a mobile device or a desktop.

I know you can do this with a help of PHP class in your code and I have done that in the past. However, for some apparent reason I couldn’t find that file.

I came across apimk, which allowed me to get the mobile information rather than me installing anything on my server.

Another neat feature about this is that all the work is being done on your client’s side rather than on your server.

For example, if you get 100,000 visitors daily, then your server is doing a lot of the heavy lifting. However, if you make this call from the client’s side, you don’t have to worry about your server working as hard.

How To Make The API Call

You can just hop onto apimk – detecting mobile device with jquery  and you will see:

 

Detecting Mobile Devices With Jquery

For the most part this is very self-explanatory and it is easy to use.

You would include your jquery at the top of your page, as you would normally do it, then just the following code to make the call:


<script type="application/javascript" src="jquery.min.js"></script>

<script type="application/javascript">
$(document).ready(function()
{
$.getJSON("http://apimk.com/ismobile?callback=json", function(data, status)
{
var d = '';
d += 'Status = '+data['Status'];
d += 'Mobile = '+data['Mobile'];
d += 'Browser = '+data['Browser'];
d += 'BrowserVersionNum = '+data['BrowserVersionNum'];
d += 'Platform = '+data['Platform'];
d += 'OS = '+data['OS'];
});
});
</script>

After you make the API call, you will get the data back in json format, which is probably the most preferable format because it is the most reliable and easy to use.

Result Back In Json Format


{"Status":"success","Mobile":"no","Browser":"GoogleChrome","BrowserVersionNum":"47.0.2526.111","OS":"Windows 7","Platform":"Windows"}

Then you could parse through the different kind of data set points.

Check the “Status” data set point to see whether it is “success” or “fail”. If its success, then you can continue parsing the data.

Next, check for “Mobile”, this will be either “yes or no”. That’s all it’s going to tell you is whether this particular device is accessing it from mobile or not.

You can also get other key information like browser, browser version number, whether the person is accessing it from Windows or whatever platform they’re accessing it from.

Sharing is caring:            

You might also like

Learn how to create HTML table to json array data

Easily Find Out The Length Of Object, Array or String

How To Create A Secure Login System With PHP And MySQL

How to detect pressing Enter on keyboard using jQuery?

Learn how to easily unfollow all of your twitter followers in 3 seconds



Did you find this helpful?