Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:38:26
Original
1580 people have browsed it

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下执行该区域代码
}
Copy after login

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>
Copy after login

For the judgment of firefox and chrome, you can expand it yourself.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!