JS to determine the current browser type, to determine the Internet Explorer method _javascript skills

Source: Internet
Author: User
The shortest ie judgment method, the original address: http://www.jb51.net/article/23621.htm

The shortest way to judge IE Browser:var Isie =!-[1,]

Principle: [1,] return "1" in standard browser, equivalent to call [1,].tostring (), return "1" in IE.
When the return value is cast to a number at this time, the standard browser returns 1,ie and returns Nan.
When the 1 and Nan are reversed, the standard browser returns False,ie returns TRUE.

JS to determine the browser type

The original text is basically no problem, but his way of judging is not very rigorous.

such as: Navigator.userAgent.indexOf ("Safari") >0, if the word "Safari" appears in the forefront of useragent, it is not impossible to get the correct results. The >0 should be changed to!=-1.
Then I tested the existing 5 standalone kernel browsers on my machine and found that Opera's useragent value was "opera/9.80" (Windows NT 5.1; U Ed ... "
It just confirms what I said.
In fact, most of the time when we do browser compatibility is mainly for IE and non-ie browsers, that is, generally speaking, only to determine whether IE browser can.
To determine the current browser type code

Copy Code code as follows:

<script type= "Text/javascript" >
<!--
function Getos ()
{
var osobject = "";
if (Isie = navigator.userAgent.indexOf ("MSIE")!=-1) {
return "MSIE";
}
if (Isfirefox=navigator.useragent.indexof ("Firefox")!=-1) {
return "Firefox";
}
if (Ischrome=navigator.useragent.indexof ("Chrome")!=-1) {
return "Chrome";
}
if (Issafari=navigator.useragent.indexof ("Safari")!=-1) {
return "Safari";
}
if (Isopera=navigator.useragent.indexof ("Opera")!=-1) {
return "Opera";
}

}
Alert ("Type->" +getos ());
-->
</script>

Related Article

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.