18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Browser and Operating System Detection<br />

A common occurrence in user-agent strings is a version number with multiple decimal points (for example,<br />

Mozilla 0.9.2). This causes a problem when you are trying to compare browser versions. You already<br />

know that the parseFloat() function is used to convert a string into a floating-point number. In addition,<br />

parseFloat() works by going character-by-character through a string, stopping when it finds a<br />

non-number character. In the case of a version number with multiple decimal points, the non-number<br />

character is the second decimal point. That means using parseFloat() on the string “0.9.2” yields<br />

a floating-point value of 0.9, completely losing .2. That’s not good.<br />

The best method for comparing two versions of this type of string is to compare the value after the decimal<br />

point in each. <strong>For</strong> instance, suppose you want to determine whether 0.9.2 is greater than 0.9.1. The<br />

correct way to do this is to compare 0 to 0, 9 to 9, and 2 to 1. Because 2 is greater than 1, version 0.9.2 is<br />

greater than 0.9.1. Because you perform this operation so often when detecting browser and operating<br />

system versions, it’s logical to encapsulate this logic in a function.<br />

The function compareVersions() accept two string versions as arguments and returns 0 if they are<br />

equal, 1 if the first version is greater than the second, and –1 if the first version is less than the second.<br />

(As you saw earlier in this book, this is a very common way of representing the relationship between<br />

two versions.)<br />

The first step in the function is to convert each version into an array of values. This fastest way to do this<br />

is to use the split() method and pass in the decimal point as the character separator:<br />

function compareVersions(sVersion1, sVersion2) {<br />

}<br />

var aVersion1 = sVersion1.split(“.”);<br />

var aVersion2 = sVersion2.split(“.”);<br />

At this point, aVersion1 contains the numbers for the first version passed in, and aVersion2 contains<br />

the number of the last version passed in. Next, it is necessary to assure that the arrays have the same<br />

number of digits; otherwise, it is very difficult to compare 0.8.4 to 0.9. To do this, first determine which<br />

array has more digits, and then add zeroes to the other array. This results in 0.9 becoming 0.9.0.<br />

function compareVersions(sVersion1, sVersion2) {<br />

}<br />

var aVersion1 = sVersion1.split(“.”);<br />

var aVersion2 = sVersion2.split(“.”);<br />

if (aVersion1.length > aVersion2.length) {<br />

for (var i=0; i < aVersion1.length - aVersion2.length; i++) {<br />

aVersion2.push(“0”);<br />

}<br />

} else if (aVersion1.length < aVersion2.length) {<br />

for (var i=0; i < aVersion2.length - aVersion1.length; i++) {<br />

aVersion1.push(“0”);<br />

}<br />

}<br />

235

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!