JS to determine whether it is IE browser and give the IE version __javascript

Source: Internet
Author: User
Tags key string string indexof

Before lazy to write to judge ie version JS, because the Internet on this aspect of the code is too much, so copied from the Internet, put on the project only to find due to the timeliness of the problem, the code does not take effect. Just write one yourself. How to see the browser's kernel and other information----JS's Global object window navigator.useragent, this attribute is the information that contains the browser information, including the browser kernel we need Navigator.useragent This value is a string, you can use string IndexOf method or a regular match to verify the key string Ie11 and edge of the way to determine the difference, I will give a few graphs of this is IE11 useragent This is the edge of the useragent IE9 useragent IE8 useragent ie10

You must have found, ie11 and edge of the useragent is and ie8,9,10 difference is quite large, then use in writing JS need special judgment, the following gives me a good section of the judge is IE and give the IE version number of JS code section


        function ieversion () {var useragent = navigator.useragent;//Get browser's useragent string var IsI E = Useragent.indexof ("compatible") >-1 && useragent.indexof ("MSIE") >-1; Determine if ie<11 browser var Isedge = useragent.indexof ("Edge") >-1 &&!isie; Determine if IE's Edge browser var isIE11 = useragent.indexof (' Trident ') >-1 && useragent.indexof ("rv:11.0") & Gt
            -1;
                if (Isie) {var reie = new RegExp ("MSIE (\\d+\\.\\d+);");
                Reie.test (useragent);
                var fieversion = parsefloat (regexp["$");
                if (fieversion = = 7) {return 7;
                else if (fieversion = = 8) {return 8;
                else if (fieversion = = 9) {return 9;
                else if (fieversion = =) {return 10; else {return 6;//ie version <=7}   
            else if (Isedge) {return ' Edge ';//edge} else if (isIE11) {R Eturn 11; IE11}else{return-1;//is not ie browser}}

The return value can be obtained by calling Ieversion (), with the following values

Value Value type Value Description
-1 Number Not IE browser
6 Number IE version <=6
7 Number Ie7
8 Number Ie8
9 Number Ie9
10 Number Ie10
11 Number Ie11
' Edge ' String IE's Edge browser


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.