Home  >  Article  >  Web Front-end  >  Native js imitates jq to determine whether the current browser is ie, accurate to ie6~8_javascript skills

Native js imitates jq to determine whether the current browser is ie, accurate to ie6~8_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:38:261579browse

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

if($.browser.msie && $.browser.version==7){
//ie7下执行该区域代码
}

Native js, imitating jq writing method, 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 the judgment of firefox and chrome, you can expand it yourself.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn