/beyond/javascript/stips/article.php/3471511/HTML-Goodies-Script-Tip-Week-19.htm HTML Goodies: Script Tip: Week 19

HTML Goodies: Script Tip: Week 19

By Joe Burns

Tips! My Man...

At the same time I'm writing this Script Tip, I'm also editing a JavaScript book. Just so I can keep a timeline straight in my own mind, we'll use the script I'm currently working on in the book as our new script here.

Click Here To
Check Out The Script
And See It In Action

The script will ask the viewer what background and text colors they would like, then the page will display with those colors. Finally, the viewer will be told, in the status bar, "Here's your color background and colortext."

Up until now, the JavaScripts we've used could basically sit where you wanted the document.write statement to print to the page. With this script, we're actually concerned with where it will sit in the HTML document.

The quickest way to implement the user's background and text color requests would be to write them to the <BODY> flag.

This script's main purpose is to write the HTML document's <BODY> flag to the page so we have to make sure that the entire script sits right where the <BODY> sits on the HTML page. And of course, we need to make sure we don't write a <BODY> flag into the HTML document ourselves. We need to let the script do that for us.

Here's the code that writes the <BODY> flag.

document.write("<BODY BGCOLOR=" +color+ " TEXT=" +txtcolor+ ">")

You already know what the double quotes and plus signs mean. The double quotes surround text to be written to the page, while the plus signs surround something that will be returned from the script somehow. In this script we know that data will come from the user because we're using two prompt commands to ask for colors.

Let's set up an example. If the user enters "blue" as the page background and "yellow" as the text color, this will be written to the page as the <BODY> flag:

<BODY BGCOLOR="blue" TEXT="yellow">

And since we have placed the script so that that line is written where the <BODY> flag would normally go, it acts as the <BODY> flag for the page. Thus the command runs and we see the color combinations on the page. Very clever.

Next Week: How Did That Text Get Into The Status Bar?

Learn to write your own JavaScripts with the
HTML Goodies 30-Step Primer Series
and take the knowledge home with you in
JavaScript Goodies!

You can find many other uses for JavaScript
on your Web pages here!

  • 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