Java Reference
In-Depth Information
if (browserVersion < 9) {
document.write("Your version of Internet Explorer is too old");
} else {
document.write("Your version of Internet Explorer is fully supported");
}
}
The first of the if statements is shown in the preceding code and checks to see if the user has IE.
If true, it then checks to see if the version is lower than 9. If it is, the user sees the message stating
his or her browser is too old. If it is 9 or 10, the message tells the user that his or her browser is
fully supported. Something goes wrong with this code with IE11, and you'll find out what that is
shor tly.
You do this again for Firefox, Chrome, Safari, and Opera. The versions of these browsers aren't
checked in this example, but you can do so if you want to:
else if (browserName == "Firefox") {
document.write("Firefox is fully supported");
} else if (browserName == "Safari") {
document.write("Safari is fully supported");
} else if (browserName == "Chrome") {
document.write("Chrome is fully supported");
} else if (browserName == "Opera") {
document.write("Opera is fully supported");
} else {
document.write("Sorry this browser version is not supported.");
}
On the final part of the if statements is the else statement that covers all other browsers and tells the
user the browser is not supported.
If you run this page in IE11, you'll see the message “Sorry this browser version is not supported.” At
first glance, this appears to be an error, but look at IE11's user‐agent string:
Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
There is no mention of MSIE anywhere, but for those versed in the browser maker's code words, we
know that Trident is Microsoft's rendering engine and the version is 11.0. Microsoft had very good
reasons for changing its user‐agent string with version 11, but this just drives the point home: You
cannot rely on browser sniffing beyond targeting a single browser.
summarY
You've covered a lot in this chapter, but now you have all the grounding you need to move on to
more useful things, such as interacting with the page and forms and handling user input.
You turned your attention to the browser, the environment in which JavaScript exists. Just
as JavaScript has native objects, so do web browsers. The objects within the web browser,
and the hierarchy in which they are organized, are described by something called the browser
 
Search WWH ::




Custom Search