/beyond/javascript/stips/article.php/3472051/HTML-Goodies-Script-Tip-Week-68.htm HTML Goodies: Script Tip: Week 68

HTML Goodies: Script Tip: Week 68

By Joe Burns



     This week, it's a re-do of the color script so that it will run in Netscape Navigator. It took a few major changes, but it's nothing we can't roll through in one tip.

Here's the Effect

Here's the Code
It's long, get it all.

     Alright, let's just stop and think it through. In order for this script to run in Netscape Navigator(NN), you must eliminate all of the commands that NN doesn't understand. So the document.all and document.all("box").style.background goes away.

     You have to flip the script upside down for NN to run it. You see, in order for NN to run hierarchy statements, the elements must already be loaded into the browser memory. In the case of the MSIE-only script, the commands came first and then the table was built. It's opposite here. First you have to build the table, then you can post the function.

     Finally, in NN, table cells are not seen as individual items. You cannot change a specific cell in a dynamic fashion. You can, however, change the page background. We'll do that.

Lose the MSIE-Only Commands

     The biggest thing is to find an equal to the way the original author worked with document.all. It was basically a line of code that had to be true for the function to work. So I set up an If statement that would always be true. The code is right at the top of the script.

var zork = 1
if (zork==1)

     I created a variable named "zork" and set it to 1. Next I asked if "zork" was equal to 1. It is, of course, and the script goes on. The drop down menu is assigned the entire run of hex codes.

Flip the Table and Function

     Note the script. I just copied and pasted the document.write statements up above the function. It was a simple cut and paste.

Change the Page Background

     Step three is to set up the script so that it's the background that's being changed rather than the table cell. The first line of code in the function is now set up to alter document.bgColor. That's the background of the page. The user's choices are gathered just the same way as before. I didn't change that at all.

One More Slight Change

     Instead of having the page background come up white, I had it come up black since that's what the drop down menus read when the page is first loaded. I did that by adding a BGCOLOR attribute in the BODY flag.

     Now the script will run in Netscape as well as MSIE. This is actually a good way to help yourself learn JavaScript. Find a script that will only run in one browser or another. Then take the time to find out why and alter the script so it will run in both.

     Because of the make-up of the browsers I wasn't able to get the effect exact, but I got pretty close, don't you think?

Next Week: Dynamic Headlines

     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: jburns@htmlgoodies.com.

Learn to write your own JavaScripts with the HTML Goodies 30-Step Primer Series

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date