October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The IF/ELSE Condition

  • September 18, 1998
  • By Joe Burns
  • Send Email »
  • More Articles »

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 that the 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 #7? It's called the "instance." The instance is a command, something for the browser to do. In this case, it will check to make sure if this browser is Netscape or not.

Okay, 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 Time: A Fancier Browser Detect Script






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel