Getting Into JavaScript
<font size=5 color="#ff0000">WebTV JS "Bug" - Reload Page (cmd-r)</font>

Before actually getting into using JS, a distinction must be made. Java and JavaScript are not the same. JS is much simpler than Java and can be directly inserted on an HTML page. Java, on the other hand, is coded separately from an HTML page, and applications are inserted on the page as a special object called an "applet". Java is not considered an easy language for non-programmers as is JS.

Using JS on WebTV can be a confusing matter as there are two different forms of JS. Netscape Navigator and Internet Explorer are competing versions of JS. Scripts that work with a Netscape (NS) browser may not work when using an Internet Explorer (IE) browser, or the converse, because of some code alternatives. It is usual to specify the browser and the JS level when making use of JS applications. WebTV attempts to be compatible with NS, IE, and ECMA (European Computer Manufacturer's Association) versions of JS. That is where confusion can arise. Full compatability with all of these standards is impossible due to the small amount of memory in the WebTV browser. At present, there is no clear-cut guide as to what is and what is not allowed for the JS that is supported by WebTV. The axiom is: try it, and if it works it's allowed.

One thing is common with JS. There is usually an attendant "bug" when running JS which may differ from browser to browser. Many which are intrinsic to NS and IE are well documented. WebTV has its own resident "bug" which becomes manifest whenever a site containing JS is first loaded. The screen may be completely gray, or only a small amount of a faded page might be seen, or other distortions may be noted. The remedy usually is to reload the page one or more times.

JS code can be embedded into an HTML page either in the head section, which is recommended so that it will load before the HTML document, or in the body of the document. Loading the JS first insures that all variable values and functions will have been stored, enabling their immediate use. As many JS codes as is desired can be introduced using "script" tags. For WebTV this has the format:

<script language="JavaScript">
<!--
document.write("My first JS code!");
/ /-->
</script>

<noframes>
WebTV JS "Bug" - Reload Page (cmd-r)
</noframes>




(Note: It must be pointed out for this example - the code as written will display on the page where it is entered, the document. The illustration shows one of the powers of JS - taking code and placing it elsewhere.)

The actual JS coding is placed between "script" tags. The language attribute is not necessary, though its use is recommended. The level of JS supported can be identified if desired, e.g.,

<script language="JavaScript1.2">


An HTML comment field beginning with <!-- at the start of a JS code and ending with / /--> following the code is often introduced (Note: A JS error may occur if there is not a space between / and / , meaning the JS thereafter may not operate). This is so older browsers that cannot recognize JS will skip all JS code.

Sometimes an additional bit of coding is included so that browsers that cannot recognize JS will display a message. JS enabled browsers will pass it by.

<noscript>
JS Not Enabled
</noscript>


In deference to the WebTV "bug", JS code on a page has a message informing the viewer that the "bug" has bitten, and to reload the page for its proper operation.

A document object-property,i.e., document.write, was used to display the string "My first JS code" on the viewing screen.Document properties include background color, text (foreground) color, and the display or writing of the value for a variable. JS links an object and a property and requires the link to be written in this way: object.property. Thus, to have a page and text of particular colors, say white on black, the code may be given as:

<script>
document.bgColor="#000000"
document.fgColor="#ffffff"
</script>





Some programmers have noted that these are not particularly useful JS statements. They probably never worked with the limitations imposed by Page Builder, where there are only four choices of solid color background: red, green, blue, and grey. Any other choice in a body tag can be seen in edit-mode, but upon going to Preview-mode only the background selected upon "Create" is shown. Now, using JS, a choice is possible. This is done using the code that follows:

In the "Heading":

   <script language="JavaScript">
   function show()
   {document.bgColor="hex code/name";
   document.fgColor="hex code/name";}
   
   <noframes>
   WebTV "Bug" - Reload (cmd-r)
   </noframes>

In the "Text":

   <body onLoad="show()">

HTML/JS follows


A JS function is all JS code appearing between { } braces. It is identified by preceding the braces by a name, "function name()". Any further use of the named function gives the result of the JS code at the point where it was named in a JS statement. Certain "events", like "load" or "click", can be used as an attribute with "event handlers". For the events cited these would be "onLoad" and "onClick", respectively. When the event is triggered by the action specified the outcome is the result of whatever function it was equated to.

The status bar in the lower left of the screen is a window object and one of its properties is "status". Display of a message in the status window is done using the coding:

<script>
window.status="Getting into JS"
</script>



In this and all subsequent sections where "hands-on" testing of JS is suitable two JS test-bed links will be provided. Use the one found to be the more comfortable. This will hopefully eliminate the need to interrupt a section in order to access WebTV's Page Builder and to have to follow all the steps needed there to make trial runs.

There are a number of situations that can occur with WebTV's Page Builder when using JS. These include power-offs in attempting to edit due to the omission of an </script> tag or the dropping, by the browser under the same circumstances, of essential brackets in JS like a < or a >. Be forewarned. JS may, and will, create problems while coding that were never encountered when using HTML.

JS Test-Bed 1 - - JS Test-Bed 2



<< PreviousNext >>