4

I'm working on a site that uses media queries. I can see in my desktop browser that they are working correctly, but when I navigate to the site on my WP8 device, no CSS is loaded. I've created a very simple HTML page to replicate the problem and show what solution I tried, but couldn't get to work.

Here is the entire code:

<html>
    <head>
        <title>WP8 Test</title>

        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

        <style type="text/css">

            @-webkit-viewport{width:device-width}
            @-moz-viewport{width:device-width}
            @-ms-viewport{width:device-width}
            @-o-viewport{width:device-width}
            @viewport{width:device-width}

            @media screen and (min-width: 1024px) {
                body {
                    background-color: red;
                }
            }

            @media screen and (min-width: 768px) and (max-width: 1024px) {
                body {
                    background-color: blue;
                }
            }

            @media screen and (min-width: 480px) and (max-width: 768px) {
                body {
                    background-color: green;
                }
            }

            @media screen and (max-width: 480px) {
                body {
                    background-color: yellow;
                }
            }

        </style>

        <script type="text/javascript">

            if (navigator.userAgent.match(/IEMobile\/7\.0/)) {
                var msViewportStyle = document.createElement("style");
                msViewportStyle.appendChild(
                    document.createTextNode(
                        "@-ms-viewport{width:auto!important}"
                    )
                );
                document.getElementsByTagName("head")[0].
                    appendChild(msViewportStyle);
            }

        </script>

    </head>
    <body>
        text
    </body>
</html>

As you can see, it is very simple, there are 4 different "breakpoints" where the body's background color will change for different screen widths. After noticing how it doesn't work in IE on my WP8 device (a Lumia 822), I began googling the issue, and it seems to be a pretty well known issue. So the solution I found, and tried, came from here:

http://timkadlec.com/2013/01/windows-phone-8-and-device-width/

It seems pretty straightforward, in that I add the five lines to the top of my CSS:

@-webkit-viewport{width:device-width}
@-moz-viewport{width:device-width}
@-ms-viewport{width:device-width}
@-o-viewport{width:device-width}
@viewport{width:device-width}

And then add some JS to detect the IE Mobile browser from the UA. I have two issues with that:

First - When I alert my user agent string, I get the following from my WP8 device:

Mozilla/4.0 (compatible; MSIE 7.0;Windows Phone OS 7.0; Trident/3.1;IEMobile/7.0;NOKIA; Lumia 822

According to the article above, and this article, it should be IEMobile/10.0 instead. Any ideas on why mine is different? This appears to be the Windows Phone 7 user agent string. Why would my WP8 device show that?

Because of that difference, I had to change the JS to match 7.0 instead of 10, otherwise the if condition would never be met.

So, even still, with my code above, nothing happens, and my screen loads white on my WP8 device. Can anyone see what I'm doing wrong?

UPDATE:

It appears the JS was throwing an error:

Unexpected call to method or property access

So I found a solution for that, here:

if (navigator.userAgent.match(/IEMobile\/7\.0/)) { 
   var s = document.createElement("style");
   s.setAttribute('type', 'text/css');
   var cssText = "@-ms-viewport{width:auto!important}";
   if(s.styleSheet) { // IE does it this way
    s.styleSheet.cssText = cssText
   } else { // everyone else does it this way
    s.appendChild(document.createTextNode(cssText));
   }

document.getElementsByTagName("head")[0].appendChild(s);
} 

But, even now that the code executes successfully, my media queries are still ignored and I still see a white page load. Any ideas?

UPDATE 2:

I found another article, here (scroll to bottom), that says as of the GDR3 update (which I have, through the dev program):

Great news! Microsoft have fixed the viewport issue in WP8 Update 3 (GDR3).

Using device-width in a CSS @-ms-viewport rule now correctly renders pages at the device-width, instead of the resolution width.

So, again I tried removing the Javascript, and adding only this to the top of my CSS:

@-ms-viewport{
   width:device-width
}

But again, no CSS loads.

UPDATE 3:

It appears my User Agent may be correct. When I navigate to whatsmyuseragent.com on my WP8 device, it shows the correct UA. Maybe it has something to do with it being a local IIS 8.0 site when it reports the incorrect one? If that's the case am I not able to test my site locally from my Windows Phone? Has anyone ever done this?

Community
  • 1
  • 1
lhan
  • 4,585
  • 11
  • 60
  • 105
  • I just checked UserAgent send by my WP8 IE and it contains both `Windows Phone 8.0` and `IEMobile/10.0`. – MarcinJuraszek Nov 25 '13 at 00:26
  • Yeah. I'm still stumped on that. I can't find anything online about a WP8 device returning a WP7 UA string, but on my Lumia 822, when I alert out the `navigator.userAgent` I always get the WP7 string from above in my post? Any ideas? – lhan Nov 25 '13 at 00:31
  • Looks like If I go to whatsmyuseragent.com it shows correctly? Could be something to do with testing on a local IIS 8.0 site? – lhan Nov 26 '13 at 01:15
  • i've the same problem with Nokia Lumia 820. I had the viewport meta, the @-ms-viewport and the javascript hack... but the page still showed not responsive and media queries don't applied. Have you found a solution? – Zauker May 04 '14 at 20:39
  • I decided it must have had something to do with testing a site on my local IIS. I have no way to tell now as my phone has been updated with IE 11 on Windows Phone 8.1. I'm also using bootstrap now for the site I was working on instead of my own media queries, and I haven't had any issues since. – lhan May 04 '14 at 22:03

1 Answers1

3

It looks like you've sorted it now with bootstrap, but it's possible the site kicked in to EmulateIE7 (for compatibility) for some reason. It seems Lumia can also pick up on this for example if you have the <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> tag and of course media queries are not supported on IE7.

superjaz1
  • 436
  • 4
  • 11
  • I do wish search engines would improve - took me no less than about 4 hours to reach this question, and your answer, through a deluge of similar but not the same problems. Virtual beer for you. – tim.baker Oct 07 '14 at 13:09