1

Before voting for duplicate:

Please read the full question below. I explained there why any of "how to detect mobile device?" is not a case here.

Question (and why it's not a simple negation of mobile dev. detection):

There are plenty of questions about "How to detect an mobile device with JavaScript?" (one of the best, if you are looking for it: What is the best way to detect a mobile device in jQuery?), but I want to ask for something a bit different: "How to detect non-mobile device with JavaScript?".

You might think that when I can make an mobile detection like this:

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

     alert('mobile');
}

I can just put negation before the statement inside if and that would be check for non-mobile device. Not really.

The code above detects several versions of mobile devices. For all not recognized devices, we cannot be 100% sure that they are not mobile. If userAgent contains one of the typed literals (webOS, iPone etc.) it's surely a mobile device. If not, it can goes two ways:

  1. It's not a mobile device
  2. It's a mobile device that we did not listed (mistake, exotic device, device released after we wrote the code etc.)

So instead of negating the test for mobile device, I want to write a test for non-mobile (Linux/Windows PC, Mac etc.). That way, if the result will be true, I will be 100% sure that I deal with non-mobile device. In the other case - it's rather mobile, but it's only an strong assumption.

So basically, I want to react only when I'm 100% sure it's non-mobile device, and when I do not know it for sure, I don't want to take any steps (or I will assume it's a mobile device).

How would that formula look? It's safe to test against Windows (Windows Phone or something like that could also use that literal?)? What about Mac, Linux PC and others?

Community
  • 1
  • 1
PolGraphic
  • 2,941
  • 9
  • 39
  • 100
  • Sorry, deleted. my mistake. – Avi L Nov 03 '14 at 15:50
  • @Avisho No problem. For any other people that would consider it as duplicate to any of "how to detect mobile device?" questions - read my the question - I give the arguments, why it's not the case here. – PolGraphic Nov 03 '14 at 15:52
  • @PolGraphic Why do you need to detect if it's a mobile device? I was under the assumption it's still bad practice to alter your site based on browser detection and instead you should use feature support detection – AeroX Nov 03 '14 at 16:55
  • @AeroX because in my case, it's not about a feature support but some properties that are impossible or very hard to detect without doubt. Just for example (but not only): pixels per inch, real screen size (in inches or centimeters). On PC I can assume that I deal with "acceptable" screen size (in centimeters) for my purposes. I cannot rely on px resolution (some mobile dev have great px resolutions and still screens smaller then standard PC). I hope I've explained myself, I don't want to make a book from it ;) P.S. I detect non-mobile device (not mobile) and other case I assume it's mobile. – PolGraphic Nov 03 '14 at 17:11

1 Answers1

-1

This is taken from here:

var BrowserDetect = function() {
    var nav = window.navigator,
    ua = window.navigator.userAgent.toLowerCase();
    // detect browsers (only the ones that have some kind of quirk we need to work around)
    if (ua.match(/ipad/i) !== null)
        return "iPod";
    if (ua.match(/iphone/i) !== null)
        return "iPhone";
    if (ua.match(/android/i) !== null)
        return "Android";
    if ((nav.appName.toLowerCase().indexOf("microsoft") != -1 || nav.appName.toLowerCase().match(/trident/gi) !== null))
        return "IE";
    if (ua.match(/chrome/gi) !== null)
        return "Chrome";
    if (ua.match(/firefox/gi) !== null)
        return "Firefox";
    if (ua.match(/webkit/gi) !== null)
        return "Webkit";
    if (ua.match(/gecko/gi) !== null)
        return "Gecko";
    if (ua.match(/opera/gi) !== null)
        return "Opera";
    //If any case miss we will return null
    return null;
};

It's not very elegant but you can add/remove it to your preference.

A second answer, while I understand you are specifically looking for a NON mobile detection, you could also use http://detectmobilebrowsers.com/ which has almost complete mobile device detection (Even the less popular).

Nathan
  • 24
  • 2
  • It's not really a case. Your code detects the browser regardless of mobile/non-mobile. And using detectmobilebrowsers.com and negation is just more complex version of the code I posted in question. Still, I want to test literally against non-mobile platforms (Mac, Windows PC, Linux PC etc.) and not negating the list of mobile platforms (why? I pointed the difference in my question). Anyway, I'm not interested in browser testing (Firefox, IE, Opera etc.) – PolGraphic Nov 03 '14 at 16:38
  • Perhaps something like this then? `var OSName="Unknown OS"; if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows"; if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS"; if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX"; if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";` – Nathan Nov 03 '14 at 17:33
  • won't "Win" return true also for Windows Phone (mobile device)? Just asking, because I cannot check it right now. – PolGraphic Nov 03 '14 at 17:40