ccdavies
ccdavies

Reputation: 1606

Device pixel ratio undefined

I am using javascript to see a cookie to the device pixel ratio. In IE device pixel ratio outputs undefined, so I want to use a conditional to say, if undefined set as 1.

I have been trying a few different approaches, but thought this would work:

<script>
                    if (window.devicePixelRatio==undefined) {
                        document.cookie='screenpixelratio='1'; path=/';location.reload(true);
                    } else {
                        document.cookie='screenpixelratio='+window.devicePixelRatio+'; path=/';location.reload(true);
                    }
                </script>

But, I cant get it to output 1.

Does anyone know how I can achieve this?

Upvotes: 0

Views: 1872

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074445

PaulJ points out in the comments on the question that you have a syntax error in your code:

document.cookie='screenpixelratio='1'; path=/';location.reload(true);
// Here --------------------------^

The ' terminates the string, and so the 1 is an invalid token at that point. You probably meant:

document.cookie='screenpixelratio=1; path=/';location.reload(true);

If that's not it, my original answer was:

I'd've thought that would work, but this may work better:

if (typeof window.devicePixelRatio === "undefined") {

Technically, it's possible for something to be undefined but not the same undefined as the one you're using to compare it to, but this is typically only in cross-window situations.

Or this may get more directly to the point (finding out whether there's a devicePixelRatio property on window):

if (!('devicePixelRatio' in window)) {

Upvotes: 1

Related Questions