Navigator Object
JavaScript Navigator Object
The JavaScript navigator object is useful for customizing your JavaScript based on the user’s browser and what they have enabled on that browser. Remember all browsers are different and handle JavaScript differently. For example, the user might not have cookies enabled, but has JavaScript enabled. Let’s take a better look at what I am talking about through examples.
While I still express my rage for some browser incompatibility, it is much better than it used to be. This ultimately means that things like the navigator object are losing their importance. Back in the day, you would use the navigator object to structure your code. So, if the user was on Internet Explorer, use this code, but if they are on Safari, use this code. The navigator is now more commonly used to track information about the user, which can also be attained by server-side programming languages adding to the lower importance of the navigator object.
Navigator Properties
- appCodeName – gets the code name of the browser
- appName – gets the name of the browser
- appVersion – gets the version
- cookieEnabled – tells us if cookies are enabled
- platform – gets the browser’s platform
- userAgent – gets the header from the server request
Example
<script type="text/javascript">
document.write(navigator.appCodeName + "<br>");
document.write(navigator.appName + "<br>");
document.write(navigator.appVersion + "<br>");
document.write(navigator.cookieEnabled + "<br>");
document.write(navigator.platform + "<br>");
document.write(navigator.userAgent + "<br>");
</script>
Result
Mozilla
Netscape
5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
true
MacIntel
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Mozilla
Netscape
5.0 (Windows)
true
Win32
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.3) Gecko/20100101 Firefox/10.0.3
I don’t usually deal with the navigator properties, unless I am setting JavaScript cookies or am gathering information about my user’s browsers. Last, but not least, I will show you the only useful method in the navigator class.
Navigator Method
javaEnabled() – tells us if Java is enabled
Example
document.write(navigator.javaEnabled());
Result
false
true