Native js imitation jq to determine whether the current browser is ie accurate to ie6~8

  • 2020-03-30 03:48:27
  • OfStack

Friends familiar with jq may occasionally use it to determine whether the current browser is ie, or even which version of ie. For example, determine the current browser is ie7, write as follows:


if($.browser.msie && $.browser.version==7){
//Ie7 under the execution of this area code
}

Native js, copy jq writing, specific implementation code:


<script>
var browser = (function(){
var isIE6 = /msie 6/i.test(navigator.userAgent);
var isIE7 = /msie 7/i.test(navigator.userAgent);
var isIE8 = /msie 8/i.test(navigator.userAgent);
var isIE = /msie/i.test(navigator.userAgent);
return {
msie:isIE,
version:function(){
switch(true){
case isIE6:return 6;
case isIE7:return 7;
case isIE8:return 8;
}
}()
};
})();
alert(browser.msie);
alert(browser.version);
</script>

For firefox, chrome, can be their own extension.


Related articles: