The Navigator Object
By Stephen Bucaro
The navigator object can be used to report information about the user's web browser
and what computer or device they are using to visit your website. Shown below are some
of the most useful navigator object properties.
Property | Description |
appCodeName | code name of browser |
appName | browser name |
appVersion | browser version |
cookieEnabled | are cookies enabled? |
platform | computer or device |
userAgent | information about itself browser sends to website |
Shown below is code that displays some of the navigator object property values
in a message box.
<script type="text/javascript">
txt = "Browser CodeName: " + navigator.appCodeName + '\n';
txt+= "Browser Name: " + navigator.appName + '\n';
txt+= "Browser Version: " + navigator.appVersion + '\n';
txt+= "Cookies Enabled: " + navigator.cookieEnabled + '\n';
txt+= "Platform: " + navigator.platform + '\n';
txt+= "User-agent header: " + navigator.userAgent + '\n';
alert(txt);
</script>
Using Navigator User-agent Property to Detect Mobile Devices
Because mobile devices have only a small screen some websites redirect their webpage
requests to a webpage specialy designed for small screens. Shown below is code that
examines the navigator object's user-Agent property to detect some mobile devices and
redirect the web browser.
<script type="text/javascript">
if ((navigator.userAgent.match(/iPhone/i)) ||
(navigator.userAgent.match(/Android/i)) ||
(navigator.userAgent.match(/iPod/i)))
(navigator.userAgent.match(/Windows Phone OS 7/i)))
{
location.replace("http://www.domain.com/special_page");
}
</script>
The code uses regular expressions to find mobile devices names in the user-Agent
string. The i option means the expression is case-insensitive.
There are many more user-Agent properties than listed here, but there is no
standard as to which ones are available in different browser versions, and the data
contained in the properties is also unreliable.
More Java Script Code: • Java Script alert Message Box • Java Script Trigonometric Methods • Java Script Reserved Words • JavaScript Math Object • Java Script Trigonometric Methods • Format a Number as Currency • Use moveBy Method to Move Window by a Specific Offset • Comparison Operators • A Brief History of JavaScript • The Screen Object
|