Instance
-
Detect browsers and versions
-
Use JavaScript to check the name and version of a visitor's browser.
-
Detect more information about the browser
-
Use JavaScript to detect more information about the visitor's browser.
-
Detects all browser Information
-
Use JavaScript to detect all information about the visitor's browser.
-
Remind users based on browser type
-
Use JavaScript to check the browser name and version of the visitor, and then generate a warning box for different content based on the information.
Browser Detection
Almost all the code in this tutorial can be run in any browser that supports JavaScript. However, some code cannot run on specific browsers, especially older browsers.
Therefore, it is helpful to check the browser type and version of a visitor, and then provide appropriate information for the visitor.
To do this, the best way is to make your web page smart enough so that it can treat different types of browsers in different ways.
Javascript contains an object named navigator, which can complete the preceding tasks.
Navigator contains information about the visitor's browser, including the browser type and version.
Navigator object
The Javascript navigator object contains all information about the visitor's browser. Next, we will learn the two attributes of the navigator object.
-
Appname
-
Save the browser type
-
Appversion
-
Contains the version information of the browser (one of other information)
Instance
navigator.appNamevar b_version=navigator.appVersionvar version=parseFloat(b_version)document.write("Browser name: "+ browser)document.write("<br />")document.write("Browser version: "+ version)</script></body>
The browser variable in the preceding example contains the name of the browser, for example, "Netscape" or "Microsoft Internet Explorer ".
In the above example, the string returned by the appversion attribute contains more than the version number, but now we only focus on the version number. We use a function named parsefloat () to extract a character in a string similar to the decimal number and return it. In this way, we can extract the version number from the string.
Important: the version number is incorrect in IE 5.0 and later versions! In IE 5.0 and IE 6.0, Microsoft assigned a value of 4.0 to the appversion string. How can this problem occur? In any case, we need to be clear that the Javascript versions in IE6, ie5, and ie4 are the same.
Instance
The following script displays different warnings based on the visitor's browser type.
var browser=navigator.appNamevar b_version=navigator.appVersionvar version=parseFloat(b_version)if ((browser=="Netscape"||browser=="Microsoft Internet Explorer")&& (version>=4)) {alert("Your browser is good enough!")}else {alert("It's time to upgrade your browser!")} }</script>