I am trying to devise a cross-browser solution in order to get the current device width in pixels in javascript in order to correspond to bootstrap breakpoints for tablet and mobile devices. (There is a reason why I am doing this, since this logic is bound to detecting device orientation, so humor me.) What would the best modern solution be for detecting this and producing consistent values on Safari, Edge, Chrome, and Firefox?
Here are the javascript methods that I am using to detect this so far. I am using jquery to detect device width so far.
export function isMobileBrowser(){
let windowWidth =
$(window).width();
let check = windowWidth <= 575.98 //bootstrap small device widths, mobile phones
return check;
}
export function isTabletBrowser(){
let windowWidth =
$(window).width();
let check = windowWidth <= 991.97 //bootstrap medium device widths, tablets.
return check;
}
Note: I have tried using user agent, but I have found that this might not be the most reliable answer. (Honestly, the StackOverflow post containing the comment regarding this issue was old, but I don't want take any risks. Here is that thread just incase your curious: Detect if device is tablet).