navigator objec...LOGIN

navigator object

navigator

  • The navigator object represents browser information. The most commonly used attributes include:

  • navigator.appName: browser name;

  • navigator.appVersion: browser version;

  • navigator.language: browser setting language;

  • navigator.platform: operating system type;

  • navigator.userAgent: User-Agent string set by the browser.

<html>
<head>
    <script>
        'use strict';
        alert('appName = ' + navigator.appName + '\n' +
                'appVersion = ' + navigator.appVersion + '\n' +
                'language = ' + navigator.language + '\n' +
                'platform = ' + navigator.platform + '\n' +
                'userAgent = ' + navigator.userAgent);
    </script>
</head>
<body>
</body>
</html>

Please note that the navigator information can be easily modified by the user, so the value read by JavaScript is not necessarily correct. In order to write different codes for different browsers, many beginners like to use if to determine the browser version, for example:


var width;

if (getIEVersion(navigator.userAgent) < 9) {
    width = document.body.clientWidth;
} else {
    width = window.innerWidth;
}

But this The judgment may be inaccurate and it is difficult to maintain the code. The correct way is to make full use of JavaScript's feature of returning undefined for non-existent properties, and directly use the short-circuit operator || to calculate:

var width = window.innerWidth || document.body.clientWidth;
Next Section
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript"> var browser=navigator.appName; var b_version=navigator.appVersion; document.write("Browser name"+browser); document.write("<br>"); document.write("Browser version"+b_version); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware