Using JavaScript to detect browsers

Source: Internet
Author: User
Tags google developer tools

assuming you really need to detect the browser type, using JavaScript is very easy to achieve.

View Demo

Download Source from GitHub


JavaScript has a navigator standard object that includes information about the browser's use.

Navigator objects are made up of very many properties. But the UserAgent property---A string that already includes the browser, the operating system, and all the other information we need.


Assume navigator.userAgent the value that needs to be displayed. Simply choose one of the following ways to:


Alert

Display in an alert Boxalert (navigator.useragent);


The navigator.useragent value of Firefox 30 on Win7.


document.write


Console.log

Display it in the browser's developer tool//This was ideal//use Console.log () when you ' re developing/experimenting Jav AScriptconsole.log (navigator.useragent);

For IE11, output such as the following

mozilla/5.0 (Windows NT 6.1; WOW64; trident/7.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0; MASM;. net4.0c;. net4.0e; rv:11.0) Like Gecko

As you can see. The problem with using Useragent.navigator is. It is a string of very long strings and is not readable.

So. Suppose I want to get the information I want, or to show it to the user, I first, I want to parse the string. The problem is that I am powerless to use the same form of expression (in some other ways). So I am very happy to use Darcy Clarke write Detect.js JavaScript library.


Detect.js can parse a string into a readable and actionable JavaScript object. To display the browser name, version number, and operating system used, refer to the following code, for example:

Create ' user ' object that would contain detect.js stuff//call Detect.parse () with Navigator.useragent as the Argumentva R user = Detect.parse (navigator.useragent);//Display Some property values in my browser ' s dev Tools consoleconsole.log (
   user.browser.family  user.browser.version  user.os.name);

In Firebug, you will see:

Firefox Windows 7

On the same machine. The results in the Google developer tools are:

Chrome Windows 7

The ability to use conditional statements for a specific browser, such as: just want to target the Safari desktop browser


if (user.browser.family = = = ' Safari ') {  alert (' you\ ' re using the Safari browser ');   }

All parsed property sheets:

Note: If the property cannot be parsed, its value is null or undefined. Suppose you want to give this information to your users, then you should infer the local conditions for values that may appear null or undefined.





Using JavaScript to detect browsers

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.