[Javascript] 判斷是不是IE 包含 Edge

2016-01-28

簡單判斷是不是IE 的Javascript Code , 因為朋友問就紀錄一下

        function DetectIsIE() {
            var ua = window.navigator.userAgent;

            var msie = ua.indexOf('MSIE ');
            if (msie > 0) {
                // 回傳版本 <=10 的版本
                return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
            }

            var trident = ua.indexOf('Trident/');
            if (trident > 0) {
                // 回傳版本 >=11 的版本
                var rv = ua.indexOf('rv:');
                return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
            }

            var edge = ua.indexOf('Edge/');
            if (edge > 0) {
                // 判斷Edge
                return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
            }

            // other browser
            return false;
        }

        var q = DetectIsIE();

        if (q != false) {
            // alert('是IE');
            window.location = 'http://google.com';
        } else {
            alert('不是IE');
        }

給有遇到有需要朋友

參考網址:
http://stackoverflow.com/questions/10964966/detect-ie-version-prior-to-v9-in-javascript


當麻許的超技八 2014 | Donma Hsu Design.