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> |
| JavaScript | Result | |
|---|---|---|
|
<SCRIPT LANGUAGE="JavaScript" > <!-- document.write ("Hello, World!!") --> </SCRIPT > |
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 > |
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.
| 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> |
Remember: You can not allow any HTML tags in the script program except those that appear within the document.write(" ") statement.
| 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> |
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.
Next Page Zip Test