HTML Goodies: Script Tip: Week 13
Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js
Date: 1/31/2018 @ 2 p.m. ET
Okay, you were nice enough to go through the smaller browser detect script with me, now let's hit a larger and much more involved browser detect script. Both scripts do the same thing, they look at the browser the user is running and send the user to a specific page.
This larger script is better than the dinky one I wrote in two ways: First, it looks not only at the "navigator.appName", but also at the navigator version number. Second, there is a catch-all at the end that sends the browser to a page if it is none of the above. Mine didn't have that. I just assumed if the browser wasn't Netscape Navigator, then it must be Internet Explorer. This doesn't assume. It has code that will send a third browser to a more text-based version of the page.
To start with, let's just look at the general concept of the script. Click back and forth to follow along. The author has been nice enough to include a great deal of comments along the way which will make this much easier.
- First, a variable "r" is created, but not given any actual value.
- Second, the variable "browser" is created so you don't have to keep writing navigator.appName again and again.
- A variable for version is set up. In the first case it's "4.0 (compatible; MSIE 4.0b1; Windows 95)."
- Now the IF statement: If the browser is MSIE and the navigator.appVersion (the equal to navigator.appName except this one returns the version) is equal to the variable "version" we just set up, then...
- Go to the parent.location.href msiepage.html.
- And finally, if all this is true, say that "r" is equal to 2.
Next, there is the Else statement that will happen if the above If statement is not true.
But what if neither of them are true? Then the script keeps reading along. Notice the If statements keep coming. There are seven more.
The only difficult part of this script is knowing what version numbers are out there. You already know that "Netscape" denotes the Navigator browser. Well, now you can see that "Microsoft Internet Explorer" denotes the MSIE browser. The easiest way to get the correct text for the navigator.appVersion is to look at the "About" menu item under Help on your browser. Luckily, this author was nice enough to find that format for us.
Read through the script. You'll see exactly what's happening along the way, until you get to the last block of testing script. It starts with this line: if (r !=1 && r !=2).
That's the catch-all. It's saying, "If it isn't any of the above, then do this." Notice it deals with that "r" variable I seem to have just glossed right over. I'll explain what it means next time.
Next Week: That "r" Variable
HTML Goodies 30-Step Primer Series
and take the knowledge home with you in
on your Web pages here!