Created
March 8, 2018 09:20
-
-
Save nhoizey/b0ffd6a471ead47532ef766e0a409794 to your computer and use it in GitHub Desktop.
Get screen density and viewport width, useful for Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get device pixel ratio in dppx | |
// https://github.com/ryanve/res/blob/master/src/index.js | |
var screen_density = | |
typeof window == 'undefined' | |
? 0 | |
: +window.devicePixelRatio || | |
Math.sqrt(screen.deviceXDPI * screen.deviceYDPI) / 96 || | |
0 | |
// keep only 3 decimals: http://jsfiddle.net/AsRqx/ | |
screen_density = +(Math.round(screen_density + 'e+3') + 'e-3') | |
// get viewport width | |
// http://stackoverflow.com/a/8876069/717195 | |
var viewport_width = Math.max( | |
document.documentElement.clientWidth, | |
window.innerWidth || 0, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment