Tutorial 16: Client-Side Scripting 101 - HTML with Style | 7

Tutorial 16: Client-Side Scripting 101

Providing alternate content

One of the major problems with scripts is that they usually append
to the document they are attached to. As we have said before, this
information can be useful but not essential. The question that arises,
however, is how to provide alternate information for browsers that do
not execute scripts.

One way would be to hide HTML fragments inside scripts, but that
would present problems with a browser that does understand
scripts but not a particular script, or a browser whose user
has turned scripting off. Such a browser would not process the
contents of the SCRIPT element at all. The answer lies in the
NOSCRIPT element.

The NOSCRIPT element

Context:

The NOSCRIPT element is a block element.

Contents:

The contents of the NOSCRIPT element are processed
only if a script was not executed earlier in the document.

Tags:

Both the start- and end-tags are required.

The NOSCRIPT element has a double function. First of all,
user agents that do not understand SCRIPT will probably also
not understand NOSCRIPT, and will thus render its
contents. Secondly, if a user agent does understand scripts
but does not execute a particular script for some reason, it should
render the contents of the NOSCRIPT element.

This is the general wording in the HTML 4.0 specification, and is
more than a bit vague and unpredictable. Besides, if you use the
method recommended above to check capabilities of a browser in a
script, the script will technically have executed. So what do you
do?

It all depends on what your script does. Most of the time, just
omitting a script will have no adverse effects on your document. In
cases where it does, my favourite technique is to actually include the
non-scripted information in your document and use the script to hide
it. For instance, imagine you have a script that prints "Good
morning!", "Good afternoon!" or "Good evening!" depending on the time
of day. What about browsers that don't run the script?

The first scenario is not to care, because you feel that this
greeting is just an interesting gimmick and adds little information to
your page (gets my vote, this one). But what if you always want a
greeting, and would like to use something generic, like "Hello!" for
people without scripting?

One way to do this would be to put "Hello!" in a NOSCRIPT
element. However, if some other script failed to execute earlier in
the page, you'd get a double greeting; as you would for browsers that
don't understand NOSCRIPT but understand SCRIPT (yes,
there are some of those around, too). Not to mention the problems that
arise from NOSCRIPT being a block element; what if the greeting
was inside a paragraph?

What I would do, was use something like the following:

<SPANID="greeting">Hello!</SPAN>

Now this greeting will be displayed by all browsers; however, in
your script, you can have something that applies the following CSS
ruleset to the document:

SPAN#greeting{display:none;}

This gets rid of the standard greeting for script-enabled browsers,
but preserves document strucure if you remove the script. This
approach also has its problems (e.g. what if the browser understands
scripts but not style sheets?) but is very good for most "Dynamic
HTML" effects ("Dynamic HTML", or "DHTML" is actually a misnomer, and
should probably be called "Dynamic Styles"; it is the sum of
techniques that use scripting languages to dynamically alter the
presentation of documents for interesting effects. DHTML effects
usually depend on CSS). A good rule of thumb, however, is that if your
script requires alternate content, it shouldn't be there in the first
place.

URL: http://www.webreference.com/html/tutorial16/6.html

Produced by Stephanos PiperoglouCreated: September 15, 1999Revised: September 27, 1999