How to detect a mobile device with JavaScript?
I have been asked to create an actual HTML page / JavaScript to simulate detection of the mobile devices (iPhone / iPad / Android) using JavaScript code. This will then take the user to a different screen which asks them for their email address.
21 Answers 21
I know this answer is coming 3 years late but none of the other answers are indeed 100% correct. If you would like to detect if the user is on ANY form of mobile device (Android, iOS, BlackBerry, Windows Phone, Kindle, etc.), then you can use the following code:
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) < // Take the user to a different screen here. >
You would detect the requesting browsers user agent string, and then decide based on what it is if it’s coming from a mobile browser or not. This device is not perfect, and never will be due to the fact that user agents aren’t standardized for mobile devices (at least not to my knowledge).
You could get the user agent in javascript by doing this:
var uagent = navigator.userAgent.toLowerCase();
And then do the check’s in the same format as this (just using iPhone as a quick example, but others would need to be a little bit different)
function DetectIphone() < if (uagent.search("iphone") >-1) alert('true'); else alert('false'); >
You’d create a simple HTML page like so:
But I am wondering what would be start? Create a dummy webpage and embedd this coding in there. Is it html that we will use in, because as I mentioned in my code above, I have nothing to start with.
Great. Thanks for that. How about redirecting it to a different page. Do you think something like this would help? if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/Android/i))) < if (document.cookie.indexOf("iphone_redirect=false") == -1) window.location = "m.espn.go.com/wireless/?iphone&i=COMR";
Don’t forget that user agents can be changed, spoofed and may not reflect the current browser. That being said if a client spoofs/changes their UserAgent and you they the wrong view displayed it’s their own fault. There are other ways other than pure user agent although the user agent is typically the root of all detection. There is a really good (although outdated) example here github.com/ahand/mobileesp?files=1
A pretty simple solution is to check for the screen width. Since almost all mobile devices have a max screen width of 480px (at present), it’s pretty reliable:
The user-agent string is also a place to look. However, the former solution is still better since even if some freaking device does not respond correctly for the user-agent, the screen width doesn’t lie.
The only exception here are tablet pc’s like the ipad. Those devices have a higher screen width than smartphones and I would probably go with the user-agent-string for those.
Right!! I only need to detect iPhone or Android. So this shouldn’t be a problem, but just to let you know I have absolutely nothing to start with. Shall I create a dummy Webpage and embedd this! Also how would the detection be checked? Would I need to transfer the script to a phone?
if(navigator.userAgent.match(/iPad/i)) < //code for iPad here >if(navigator.userAgent.match(/iPhone/i)) < //code for iPhone here >if(navigator.userAgent.match(/Android/i)) < //code for Android here >if(navigator.userAgent.match(/BlackBerry/i)) < //code for BlackBerry here >if(navigator.userAgent.match(/webOS/i)) < //code for webOS here >
Even though this code won’t execute more than one block since it’s checking one thing over and over again, this code should make use of else-if blocks because of that reason. Also, what if the user agent variable changes somehow in between blocks? It’ll execute more than 1 block, here.
You are right ,and don’t forget that code was 4 years ago 🙂 and there is now better tools or ways to do the same job
var isMobileDevice = navigator.userAgent.match(/iPad|iPhone|iPod/i) != null || screen.width
+1 for regex matching allowing users to easily check for multiple devices in one shot. However, I would add for clarity that because you denote /i at the end of the regex for insensitive match there really is no need to camel case the search params. The following would be equivalent (and also search for android devices): .match(/ipad|iphone|ipod|android/i)
A simple solution could be css-only. You can set styles in your stylesheet, and then adjust them on the bottom of it. Modern smartphones act like they are just 480px wide, while they are actually a lot more. The code to detect a smaller screen in css is
@media handheld, only screen and (max-width: 560px), only screen and (max-device-width: 480px) < #hoofdcollumn >
I use mobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)
So I did this. Thank you all!
VIEW NORMAL SITE