Thursday, March 28, 2024

HTML Goodies: Script Tip: Week 12

Hey, Tipsters…

Now we get to the meat of this script, the IF/ELSE condition. It’s really the central purpose of this browser detection system.

     Here’s the script once again:


<SCRIPT LANGUAGE=”javascript”>

<!–Hide the Script

//This Script written by Joe Burns, Ph.D.
//Use it freely

if (navigator.appname == “Netscape”)
{parent.location=”nspage.html”}

else

{parent.location=”msiepage.html”}

// End hiding the Script–>
</SCRIPT>


Before getting into the use of the IF/ELSE within the script, let’s talk about what the words mean in plain English. IF/ELSE denotes two choices, but forces one. The second choice is a catch-all. If this is the case, do this. If that isn’t the case, then do this. That’s the basic syntax of the condition.

Here’s that syntax from above in JavaScript:

if (navigator.appname == “Netscape”)
{parent.location=”nspage.html”}
else
{parent.location=”msiepage.html”}

Let’s follow it along. If the navigator.appname (that’s the browser in JavaScript) is Netscape, then go to the page named “nspage.html.” If the navigator.appname isn’t Netscape, then (denoted by “Else”) go to a page called “msiepage.html.”

Notice I only had the script test for one type of browser, Netscape. The thinking is that if the browser isn’t Netscape Navigator, it must be Internet Explorer. I know that’s reaching a bit, but I’ll bet I’m right 90% of the time and that’s a pretty good percentage.

You should notice that that results of the IF/ELSE conditions are actual functions of the script. By function, I mean that the browser is expected to do something. In this case, send the user to either nspage.html or msiepage.html. Because of thatthe results are surrounded in {fancy brackets}, as they are functions. Get it?

I should also point out that the condition is surrounded by (parentheses). Remember that from Script Tip #7Okay, that about wraps up this script, but not quite. Next week we’ll start a new script that does basically the same thing. It will detect the users’ browser and send them to a new page depending on what type is used. The upcoming script will differ from this one in its ability to read further into the browser parameters. Not only will you be able to set a page for either Netscape Navigator or Internet Explorer, you’ll be able to set the script to read the browser’s specific version number. It gets a little hairy, but we’ll get through it.

Next Week: A Fancier Browser Detect Script

    


Learn to write your own JavaScripts with the
JavaScript Goodies!


You can find many other uses for JavaScript
on your Web pages here!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured