Java Script concepts Comments Writing to the screen Writing with variables

Java Script concepts

Java Script is one of those pseudo-languages which was developed by Netscape. It has nothing to do with Sun's Java programming language but it is useful for allowing some limited programming on either the client (your PC) or the server (the computer which hosts the website).

Script programlets are embedded in the body of HTML files and execute as they are encountered. You will note that writing code for the client to execute merely requires that you use the <SCRIPT> and </SCRIPT> tags. It is not necessary to identify the fact that you are writing java script or Visual basic script although for proper form, you might want to code the script tags as follows:
<SCRIPT LANGUAGE="JavaScript" >
Additionally, you may want to worry about browsers which can't accommodate scripting by enclosing the code within comments <!-- the code you write -->

You can also write the code in a separate file and reference it via the SRC parameter. If the script were to be used in several different pages, rather than re-writing (or copying) the script in each of the HTML pages, we merely include the script using SRC. Here's what the typical script tag with a source file would look like:

JavaScript Result
<SCRIPT LANGUAGE="JavaScript" SRC="source.js">
</SCRIPT>
Table 1 - JavaScript with source file
Top

Writing to the screen

The easiest way to write some HTML using Java Script is to utilize the "document.write" as shown:
JavaScript Result
<SCRIPT LANGUAGE="JavaScript" >
<!--
document.write ("Hello, World!!")
-->
</SCRIPT >
Table 2 - Writing to the screen
Top
It is important to remember that nothing can go out to the web page while working in the script other than by way of the document.write (also, don't forget the parentheses).

Of course, all the normal HTML tags will work with JavaScript so if we wanted to make the text bold we need only add the tag necessary for bold. For instance,

JavaScript Result
<SCRIPT LANGUAGE="JavaScript" >
<!--
document.write ("<B>Hello, World!!</B>")
-->
</SCRIPT >
Table 3 - Using HTML tags

It is important to remember to include the parentheses and the quotes. Also, note that there is no automatic break between multiple document.write statements so these statements will be rendered as concatenated text in the browser.

Top

Comments

JavaScript allows you to use C++ form of commenting. Thus, you can either use the single line comment via // or you can use the /* comments over several lines */
For example:
JavaScript Result
<SCRIPT>
// this is a comment which will be in effect until the end of the line
document.write("<B>This line will print out </B>")
/* This form of comment
will span any number
of lines and only ends when it encounters an asterisk slash
document.write("This line will never print")
*/
</SCRIPT>
Table 4 - Comments

Remember: You can not allow any HTML tags in the script program except those that appear within the document.write(" ") statement.

Top

Writing with variables

The subject of variables is long. But, if what you really want to do is to create a "constant" which can be used within the script rather than writing the whole statement within a document.write(" ") statement, you could define the constant early in the script and then simply use the constant name in the document.write(" ") statement. Here's an example:
JavaScript Result
<SCRIPT>
x="<BR/>Note that the constant is case sensitive <BR>"
Hi="Hello, World!!"
document.write(Hi)
document.write("x")
document.write(HI)
document.write("<BR/>Hi<BR/>")
</SCRIPT>
Table 5 - Writing with constants

Did you notice that not all the lines of code were rendered? The script continued to work until it encountered the "document.write(HI) which it could not resolve. The script just stops at this point and flushes to the </SCRIPT> As a matter of fact, there was a valid line afterward but it never printed. Your browser might also warn you that there was a problem and ask you if you want to debug. IE tells you the line the error occurred and says that HI is not defined.

In the above example we created a variable named Hi which was initialized to contain a string "Hello, World!!". JavaScript really doesn't care much about what contents you put into its variables. As a matter of fact, in subsequent lines of code you could put a number into Hi and do a calculation.

Top
Next Page Zip Test