Search code examples
javascripthtmlinternet-explorerwicketinternet-explorer-10

Javascript is not working in IE


I am writing a javascript, that ll get the browser information and set them in cookies, so that I can use them in future. The setCookies() is complext javascript function, that ll do some manipulation on the navoigator user agent and determine the browser.

This functions seems to work on all major browsers, but it is not working in IE (11, 10, 9, 8 , 7), I don't know why.

Other info : I am developing a Apache wicket application, version 1.4.19.

Here is the Javascript function

    <script type="text/javascript">
        function setCookies() {
            var browser;
            var version;


            var click_ev = document.createEvent("MouseEvents");
            click_ev.initEvent("click", true /* bubble */, true /* cancelable */);

            document.cookie = "user_agent=" + navigator.userAgent + "; path=/";
            document.cookie = "navigator_app_platform=" + navigator.platform
                    + "; path=/";

            var platform = navigator.platform;
            if (platform == "iPad" || platform == "iPhone" || platform == "iPod"
                    || platform == "iPod touch") { // For iPhones iPads and iPods

                var userAgent = navigator.userAgent;

                var chromeIndex = userAgent.indexOf("criOS");
                var opera = userAgent.indexOf("OPiOS");
                var ucIndex = userAgent.indexOf("UCBrowser");
                var coast = userAgent.indexOf("Coast");
                var mercury = userAgent.indexOf("Mercury");

                var safari = userAgent.indexOf("Safari");
                var webkit = userAgent.indexOf("AppleWebKit");

                // Chrome detection
                if(chromeIndex > 0) {
                    browser = "chrome";

                    var arr = userAgent.split("criOS/");
                    arr = arr[1].split(".");
                    version = arr[0];
                }
                // Safari detection
                else if(opera < 0 && ucIndex < 0 && coast < 0 && mercury < 0 && safari > 0 && webkit > 0) {
                    browser = "safari";

                    var arr = userAgent.split("AppleWebKit/");
                    arr = arr[1].split(".");
                    version = arr[0];
                } else {
                    browser = "unknown";
                    version = -1;
                }

            } else if(platform == "Win32" || platform == "MacIntel") { // For windows and mac
                // Browser detection
                var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
                // Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
                var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
                var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
                // At least Safari 3+: "[object HTMLElementConstructor]"
                var isChrome = !!window.chrome && !isOpera; // Chrome 1+
                var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6


                if (isOpera) {
                    browser = "opera";
                } else if (isFirefox) {
                    browser = "firefox";
                } else if (isChrome) {
                    browser = "chrome";
                } else if (isIE) {
                    browser = "ie";
                } else if (isSafari) {
                    browser = "safari";
                } else {
                    browser = "unknown";
                }

                if (browser == "firefox") {
                    var arr = navigator.userAgent.split("Firefox/");
                    arr = arr[1].split(".");
                    version = arr[0];
                } else if (browser == "chrome") {
                    var arr = navigator.userAgent.split("Chrome/");
                    arr = arr[1].split(".");
                    version = arr[0];
                } else if (browser == "ie") {
                    var arr = navigator.userAgent.split("MSIE ");
                    arr = arr[1].split(".");
                    version = arr[0];
                } else if (browser == "safari") {
                    var arr = navigator.userAgent.split("AppleWebKit/");
                    arr = arr[1].split(" ");
                    arr = arr[0].split(".");
                    version = arr[0];
                } else if (browser == "opera") {
                    var arr = navigator.userAgent.split("OPR/");
                    arr = arr[1].split(".");
                    version = arr[0];
                } else if (browser == "unknown") {
                    version = -1;
                }

            } else if(platform == "Android" || platform == "Linux armv7l") { // For android
                var userAgent = navigator.userAgent;

                var chromeIndex = userAgent.indexOf("Chrome");
                var opera = userAgent.indexOf("OPR");
                var ucIndex = userAgent.indexOf("UCBrowser");
                var coast = userAgent.indexOf("Coast");
                var firefox = userAgent.indexOf("Firefox");

                // Chrome detection
                if(chromeIndex > 0 && opera < 0 && ucIndex < 0 && coast < 0) {
                    browser = "chrome";

                    var arr = userAgent.split("Chrome/");
                    arr = arr[1].split(".");
                    version = arr[0];

                } else if(firefox > 0) { // Firefox detection
                    browser = "firefox";

                    var arr = userAgent.split("Firefox/");
                    arr = arr[1].split(".");
                    version = arr[0];

                } else if(opera > 0) { // Opera detection
                    browser = "opera";

                    var arr = userAgent.split("OPR/");
                    arr = arr[1].split(".");
                    version = arr[0];

                } else { // Unknown browser
                    browser = "unknown";
                    version = -1;
                }

            } else { // For other platforms
                version = -1;
                browser = "other_platform";
            }

            document.cookie = "browser_version=" + version + "; path=/";
            document.cookie = "browser_name=" + browser + "; path=/";

            document.getElementById("redirect").dispatchEvent(click_ev);
        }
    </script>

Solution

  • I did fix the issue after some playing around. It turned out that, in the newer versions of IE userAgent is not same in every versions.

    The user agent in my IE10 does not contain "MSIE", and I was trying to split the userAgent by "MSIE", and it was resulting a null pointer situation.

    So the IE version detection script changed from :

    if (browser == "ie") {
    var arr = navigator.userAgent.split("MSIE ");
    arr = arr[1].split(".");
    version = arr[0];
    }

    To :

    if (browser == "ie") {
    var v1 = navigator.userAgent.indexOf("MISE");
    var v2 = navigator.userAgent.indexOf("rv:");
    if(v1 > 0) {
    var arr = navigator.userAgent.split("MSIE ");
    arr = arr[1].split(".");
    version = arr[0];
    } else if(v2 > 0) {
    var arr = navigator.userAgent.split("rv:");
    arr = arr[1].split(".");
    version = arr[0];
    }

    Thanks :)