Determining Whether Mobile or Desktop

I found this via the AI on Google search:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
function isMobile() {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}

if (isMobile()) {
  // Code to execute if the device is mobile
  console.log("Mobile device detected");
} else {
  // Code to execute if the device is not mobile
  console.log("Not a mobile device");
}


It will determine whether a person is using a tablet or cell phone or is on a desktop computer. Then the web page can display things differently, depending.
 

Comments