Question

I have the following javascript:

    <script  type="text/javascript">
        var isMobile = {
            Android: function () {
                return navigator.userAgent.match(/Android/i);
            },
            BlackBerry: function () {
                return navigator.userAgent.match(/BlackBerry/i);
            },
            iOS: function () {
                return navigator.userAgent.match(/iPhone|iPad|iPod/i);
            },
            Opera: function () {
                return navigator.userAgent.match(/Opera Mini/i);
            },
            Windows: function () {
                return navigator.userAgent.match(/IEMobile/i);
            },
            any: function () {
                return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
            }
        };

        navigator.sayswho = (function () {
            var ua = navigator.userAgent, tem,
            M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
            if (/trident/i.test(M[1])) {
                tem = /\brv[ :]+(\d+(\.\d+)?)/g.exec(ua) || [];
                return 'IE ' + (tem[1] || '');
            }
            M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
            if ((tem = ua.match(/version\/([\.\d]+)/i)) != null) M[2] = tem[1];
            return M.join(' ');
        })();
        var browserversion1 = navigator.sayswho.split(" ");
        var browserversion2 = browserversion1[1].split(".")[0].split(",");
        var isIeLessThan10 = (browserversion1[0] == "IE" || browserversion1[0] == "MSIE") && browserversion2[0] < 10
        //alert(isIeLessThan10);

        if (!isMobile.any()) {
            if (isIeLessThan10) {
                alert("IE<10");
                document.write('<link rel="stylesheet" href="theStyles/defaultStyle_ie.css" type="text/css" charset="utf-8" />');
                document.write('<link rel="stylesheet" href="theStyles/captionStyle_ie.css" type="text/css" charset="utf-8" />');
            }
            else {
                alert("IE>=10 || !IE");
                document.write('<link rel="stylesheet" href="theStyles/defaultStyle.css" type="text/css" charset="utf-8" />');
                document.write('<link rel="stylesheet" href="theStyles/captionStyle.css" type="text/css" charset="utf-8" />');
            }
        }
        else {
            alert("mobile");
            document.write('<link rel="stylesheet" href="theStyles/defaultStyle_mobile.css" type="text/css" charset="utf-8" />');
            document.write('<link rel="stylesheet" href="theStyles/captionStyle_mobile.css" type="text/css" charset="utf-8" />');
        }
</script>

The above script checks to see if the browser is a mobile browser or full feature browser. If it's a full feature browser then check to see if IE version is less than 10 and if it is then load a defaultStyle_ie.css stylesheet otherwise if it's IE10 or higher or any other browser load defaultStyle.css stylesheet. Everything works as it should in FireFox, Chrome and IE8. When I load the page in IE10, the IE8 version stylesheet is loading. How do I fix that?

Was it helpful?

Solution

As per my comment, load the normal stylsheets.

Then add a media condition in for mobile.

And then load IE9 or less stylesheet

<!-- default stylesheets -->
<link rel="stylesheet" type="text/css" href="theStyles/defaultStyle.css">
<link rel="stylesheet" type="text/css" href="theStyles/captionStyle.css">

<!-- mobile stylesheets -->
<link rel="stylesheet" type="text/css" href="theStyles/defaultStyle_mobile.css" media='screen and (max-device-width: 480px)'>
<link rel="stylesheet" type="text/css" href="theStyles/captionStyle_mobile.css" media='screen and (max-device-width: 480px)'>

<!-- if ie version 9 or less -->

<!--[if lte IE 9]>
<link rel="stylesheet" type="text/css" href="theStyles/defaultStyle_ie.css">
<link rel="stylesheet" type="text/css" href="theStyles/captionStyle_ie.css">
<![endif]-->

OTHER TIPS

Problem is that you are in compatible mode in IE, it then sends older version. You can check the mode in development console by hitting F12

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top