SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 12

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

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!

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.