HTML Goodies: Script Tip: Week 10
WEBINAR: On-demand webcast
How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >
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:
<!--Hide the Script
//This Script written by Joe Burns, Ph.D.
if (navigator.appname == "Netscape")
// End hiding the 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 //
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 //-->
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
HTML Goodies 30-Step Primer Series
and take the knowledge home with you in
on your Web pages here!