0

The existing answer is incorrect: Detect real screen resolution (ignoring browser zoom etc.)

It will not show the screen resolution, but rather the document size.

Is there a way to get screen resolution in Firefox, even when zoom is applied?

Community
  • 1
  • 1
Valentin V
  • 22,569
  • 28
  • 97
  • 147
  • Whole debate and answer (more or less) here : https://stackoverflow.com/questions/1713771/how-to-detect-page-zoom-level-in-all-modern-browsers – Paul-Elie Pipelin Jun 22 '18 at 09:08

1 Answers1

1

OK, thanks to https://github.com/mp31415 I have a working solution:

var isIE = function () {
  if(navigator.appName === "Microsoft Internet Explorer") {
    return true;
  } else if(navigator.appName === "Netscape" && /Trident/.test(navigator.userAgent)) { // IE 11
    return true;
  }
  return false;
};
var isFF = function () {
  return !!navigator.userAgent.match(/firefox/i);
};
var sw = screen.width;
var sh = screen.height;
var zoom = 1;
if (this.isIE()) {
    zoom = screen.deviceXDPI / screen.systemXDPI;
    zoom = this.round5(zoom*100)/100;
}
else if (this.isFF()) {
    zoom = window.devicePixelRatio;
}
if (zoom != 1) {
  sh = this.round10(sh * zoom);
  sw = this.round10(sw * zoom);
}

This code returns resolution scaled for Retina displays but otherwise works fine.

Valentin V
  • 22,569
  • 28
  • 97
  • 147
  • 1
    Browser sniffing in 2015, seriously? – RobG Jan 30 '17 at 06:37
  • I don't understand why you mean by your comment :) – Valentin V Jan 30 '17 at 15:06
  • [*Feature Detection vs Browser Detection*](https://www.joezimjs.com/javascript/feature-detection-vs-browser-detection/). ;-) – RobG Jan 31 '17 at 02:59
  • ok, got it :). Some implementation differences still exist today and require browser detection. – Valentin V Feb 01 '17 at 18:07
  • @Valentin Not this one, necessarily. You could test for the existence of the pertinent properties with `typeof`, and use them only if available. – PointedEars Feb 26 '18 at 09:01
  • These comments disparaging browser detection are silly. Properties like screen.width exist in all browsers, but are scaled differently by browser vendor. This is a case where you HAVE to do browser detection because browsers have independent behavior that falls outside of a defined spec. (There is no defined spec for zooming.) – Matthew Dean Mar 04 '21 at 21:48