top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+2 votes
436 views

What is the best way to detect a mobile device in JavaScript? (Sample code would be helpful).

posted May 29, 2017 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
you can use simple javascript to detect it:

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

1 Answer

+1 vote

Thanks Mani,

I wanted certain action to be visible to the desktop or bigger screen so used the following code

if(window.innerWidth > 800 || window.innerHeight > 600) {   // Its a case of desktop 
// Desktop code 
}
else {
// Mobile code 
}
answer May 30, 2017 by Salil Agrawal
Similar Questions
+3 votes

I went through this Discussion:

http://stackoverflow.com/questions/7087036/fetching-images-from-server-while-drawing-the-cell

I have a web server server-1.example.com. There is a JSON-Server running on another server server-2.example.com. What I have to achieve is to fetch a list of URLs (in form of JSON response) from server-2 and loop through the urls and fetch them from the server-1. In the meantime i have to animate these images on the browser by Fading the images one after the another.

The algorithm is something like this:-

START:
   FETCH the URLs from server-1 and store them in a cache C
   Loop until C.length
      Fetch first K images by forking a thread.
      animate k images
 END

How can I optimize K? Is my approach right?

+2 votes

I know it is possible via IE (ActiveX objects).
Can we do this with all browsers ? (Specially in FireFox)

...