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.