SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 19

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

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