0

You can detect if the visitor uses a mobile device:

if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) {
                // here my codes }

But how to turn this code into an if not (to get all devices that are not these ones)?

Maybe something like this?

if !(/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) {
                // here my codes }

As you understand my question goes about standard syntax (I am learning).

Cinzel
  • 215
  • 3
  • 13
  • FYI, this is JavaScript, not jQuery. jQuery is a DOM manipulation framework that is implemented in the language of JavaScript. – Moby's Stunt Double Mar 03 '16 at 23:10
  • can always use `===false` ... or wrap the whole thing inside `()` and do `if(!(/Andr......i.test(navigator.userAgent)))` – charlietfl Mar 03 '16 at 23:11
  • yes @charlietfl, looks what I am searching for. If I write your solution as whole code would it be like this? `if (!/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) { // here my codes }` Maybe you can place your entire code as an answer? – Cinzel Mar 04 '16 at 08:57

2 Answers2

0

You can use an if statement with an else. The else will trigger if the condition in your if statement is not met.

if(/Android | webOS | iPhone| iPad| iPod| BlackBerry| BB| PlayBook| IEMobile| Windows Phone| Kindle| Silk| Opera Mini/i.test(navigator.userAgent)) {

  //code 

}else{

  //other code

}
kravse
  • 126
  • 1
  • 8
  • Because my question was only about "if not", does this mean I have to place my code inside the else { }, and I can leave the codes inside if { } blank? – Cinzel Mar 04 '16 at 08:55
  • Ah. I'm sorry. I misunderstood your question. You shouldn't use an if/else with a blank if. It would be better to use charlietfl's answer. Using the ! inside the if brackets is the correct method. Just an fyi though, if you're trying to detect mobile devices, you may be better off checking for a touch event (see: http://stackoverflow.com/a/4819886/6015622). Hope this helps. – kravse Mar 04 '16 at 15:08
  • Thanks @kravse for the useful suggestion. – Cinzel Mar 04 '16 at 15:25
0

Creating a variable might be best for readability

var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent);

if(!isMobile){

}
charlietfl
  • 170,828
  • 13
  • 121
  • 150