Determine the browser type and version number based on JS

Source: Internet
Author: User
Function browserinfo (){
VaR browser_name = navigator. appname;
VaR browser_version = parsefloat (navigator. appversion );
VaR browser_agent = navigator. useragent;

VaR actual_version, actual_name;

VaR is_ie = (browser_name = "Microsoft Internet Explorer ");
VaR is_nn = (browser_name = "Netscape ");

If (is_nn ){
// Upper 5.0 need to be process, lower 5.0 return directly
If (browser_version> = 5.0 ){
VaR split_sign = browser_agent.lastindexof ("/");
VaR version = browser_agent.indexof ("", split_sign );
VaR bname = browser_agent.lastindexof ("", split_sign );

Actual_version = browser_agent.substring (split_sign + 1, version );
Actual_name = browser_agent.substring (bname + 1, split_sign );
}
Else {
Actual_version = browser_version;
Actual_name = browser_name;
}
}
Else if (is_ie ){
VaR version_start = browser_agent.indexof ("MSIE ");
VaR version_end = browser_agent.indexof (";", version_start );
Actual_version = browser_agent.substring (version_start + 5, version_end)
Actual_name = browser_name;

If (browser_agent.indexof ("Maxthon ")! =-1 ){
Actual_name + = "(Maxthon )";
}
Else if (browser_agent.indexof ("Opera ")! =-1 ){
Actual_name = "Opera ";
VaR tempstart = browser_agent.indexof ("Opera ");
VaR tempend = browser_agent.length;
Actual_version = browser_agent.substring (tempstart + 6, tempend)
}
}
Else {
Actual_name = "unknown Navigator"
Actual_version = "unknown version"
}
/*------------------------------------------------------------------------------
-- Your can create new properties of Navigator (acutal_name and actual_version )--
-- Userage :--
-- 1, call this function .--
-- 2, use the property like this: navigator. actual_name/navigator. actual_version ;--
------------------------------------------------------------------------------*/
Navigator. actual_name = actual_name;
Navigator. actual_version = actual_version;

/*---------------------------------------------------------------------------
-- Or made this a class .--
-- Userage :--
-- 1, create a instance of this object like this: var browser = new browserinfo ;--
-- 2, user this instance: browser. Version/browser. Name ;--
---------------------------------------------------------------------------*/
This. Name = actual_name;
This. Version = actual_version;
}
Browserinfo ();

Document. Write ("your browser is:" + navigator. actual_name + ", version:" + navigator. actual_version );

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.