Java Reference
In-Depth Information
of one of the major browsers but not actually support all the JavaScript or BOM objects, properties, or
methods of that browser. Therefore this method of “browser sniffi ng” has fallen out of favor and is not
the recommended way of checking for compatibility. It's really a last resort when all other methods have
failed, such as when two different browsers support the same object and property but implement them
so that they work in two different ways. Object checking wouldn't help you in that circumstance, so
you'd have to fall back on using the navigator object.
The appName property returns the model of the browser, such as “Microsoft Internet Explorer” for IE,
“Opera” for Opera, or “Netscape” for Firefox, Safari, and Chrome.
The userAgent property returns a string containing various bits of information, such as the browser
version, operating system, and browser model. However, the value returned by this property varies
from browser to browser, so you have to be very, very careful when using it. For example, the browser's
version is embedded in different locations of the string.
Try It Out Checking for and Dealing with Different Browsers
In this example, you create a page that uses the aforementioned properties to discover the client's
browser and browser version. The page can then take action based upon the client's specifi cations.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 6: Example 7</title>
<script type=”text/javaScript”>
function getBrowserName()
{
var lsBrowser = navigator.userAgent;
if (lsBrowser.indexOf(“MSIE”) >= 0)
{
lsBrowser = “MSIE”;
}
else if (lsBrowser.indexOf(“Firefox”) >= 0)
{
lsBrowser = “Firefox”;
}
else if (lsBrowser.indexOf(“Chrome”) >= 0)
{
lsBrowser = “Chrome”;
}
else if (lsBrowser.indexOf(“Safari”) >= 0)
{
lsBrowser = “Safari”;
}
else if (lsBrowser.indexOf(“Opera”) >= 0)
{
lsBrowser = “Opera”;
}
else
{
Search WWH ::




Custom Search