Thursday, March 28, 2024

HTML Goodies: Script Tip: Week 10

Hello, Script Tippers…

It’s time for a new script. I’m proud to say that today we start tearing apart a script that I authored. It’s a very popular event, one that HTML Goodies readers have asked about for a long while.

This is a browser choice script. Here’s the concept: A viewer logs into your page. The page containing this script loads. The script “reads” what type of browser the viewer is using. If it’s Netscape Navigator, the viewer is sent to a page titled “nspage.html.” If the viewer is not using Navigator, they are sent to a page titled “msiepage.html”.

It all happens very quickly so the viewer barely knows what went on. But the benefits are great, especially if you want to create a page with Explorer- or Navigator-specific commands. The viewer gets to the page that is best suited for their browser.

Here’s the script:

<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>

If you’ve been following along with these Script Tips, then you already know what that If statement does. But if not, you can almost pick this script apart just by reading through the lines. We’ll get to tearing it up in the coming Tips, but first let’s look at those two strange items up there.

Double Slashes //

In JavaScript, that means it’s a comment line. As long as you have those double slashes, the line is retained and printed in the code, but is not used in the application of the script. It allows you to put in comments before a section to remind yourself of what the following does, or use it to proclaim that you’re the author. That’s what I did above.

Just remember that the double slash only allows for one line of text. If you add a line without the double slashes or the line you have somehow jumps to the next line by mistake… error!

In case you’re wondering, you can set apart an entire paragraph as a comment. Just start the text with this: /* and end it with this: */. Keep the star next to the text. Then you can write as many lines as you want. The browser will see it all as a comment rather than something to be played with.

<!– and //–>

You HTML fans probably already know these commands as comment commands themselves. They do the same thing in HTML that the /* and */ do in JavaScript. I have them in this script so that lesser-version browsers that do not understand JavaScript (yes, they are still out there) will not display the text.

By using these HTML comment commands, you make so that the script is commented out. And it does not affect the effect of the script one bit. What a wonderful world we live in, huh?

I only ask that you pay close attention to where the comment commands are, just inside the starting SCRIPT command and just inside the end SCRIPT command.

None of these comment commands are required, but they do come in handy when you need them.

Next Week: We Tear It Apart

     Do YOU have a Script Tip you’d like to share? How about suggesting a Script Tip to write about? I’d love to hear it. Write me at: webmaster@htmlgoodies.com


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