Held Monday, February 28, 2000
Question 22 for Tuesday's class: Describe an algorithm that you think would be useful for the Web.
Until the development of those languages, some of the ``features'' of
a programming language needed to be supported by HTML (or variants
thereof) and the browser. For example, there are tags in HTML to load
another page after some time progresses, or to test for browser support
of some tags. For example, there is a
<noframes> tag for browsers that don't support
parameter to the appropriate tag (body tag, a tag, input tag, etc.)
For now, many of our examples will be based on buttons and the
onClick event and will have the form
<form name="sample"> <input type="button" name="clickme" value="Click Me" onClick="..."> </form>
As you may have observed from this short template, it's helpful (and almost necessary) if you name your forms.
Since we'll need some commands to get started, we'll begin with input and output. For now, we'll simply describe the commands. In the next section, we'll see how to incorporate them into a page.
One way to provide information to the reader is with the
alert(text) function. This function brings up an
alert box which displays the argument to the function. Many
people find the alert box a simple way of providing a greeting
without "muddying" the underlying HTML.
A sample program
A similar way to obtain information from the reader is with the
prompt(request,default) function. This
function brings up a dialog box in which the reader can type a
response. In most cases, you will want to include a default setting.
The function returns the string the reader entered.
A sample program
You can also put information into fields in a form with
document.formname.fieldname.value = ...
You'll need to fill in the name of the form and the name of the field (which is why they're in italics).
A sample program
Finally, you can set the information on the status bar of the window with
This allows you to give some information to the reader without forcing him or her to respond to a dialog box. At the same time, you make sure that the information is some place that the reader will see it.
A sample program
in HTML pages, you can also save them as
and load them into navigator. This is often useful for quick testing
<SCRIPT> tag. The tag has two optional
src parameter is particularly useful, as it lets
general functions, and then incorporate them into your program using
<script src="..."> tag.
things to you as objects, and lets you manipulate them as such. For
example, you can refer to the current window as
the current document as
The pieces of information associated with an object are called the
properties of that object. A property may be a number, some
text, or even another object). To refer to a property of an object, you
use the name of the object, a period, and the name of the propety. For
example, you can obtain the title of a document as
Objects also have associated functions, which are normally called
methods. These are the things that the object knows how to do.
For example, you could tell the document to close itself with
For certain types of predefined objects, such as windows and arrays,
you create a new object using the
You assign values to variables by writing the variable name, an
equals sign, and the expression you want to assign to the variable.
alpha=2 assigns the value 2 to the variable
You declare a new variable with
var varname If the
declaration is within a function, the variable is local to that
function. If the declaration is outside of a function, the variable is
global (accessible from any function).
As you develop longer and longer programs, you should insert notes
to yourself (or to others) to help explain the program. These are
with two slashes,
//, and end at the end of a line.
2*10+3. It also permits you to use variables within
+ is used to concatenate strings.
If you happened to have the user's name stored in the variable
userName, you could create a greeting with
var greeting // A greeting to the user greeting = "Hello " + userName + ". Welcome to this page."
Up to this point, we've only been interacting with the user by putting
up dialogue boxes or inserting information in fields. Clearly, we'd
like to do more. For example, we might want to insert text or HTML into
method of the
document object. That is, you use
document.write(text) to insert text. The text is
document.writeln(text) to write the text an add a
carriage return to the end of the line.
For example, we might create a page that greets a user by name with
Hello " + userName + " welcome to my page.") </script>
Note that I've included spaces in the strings so that the user's name does not run into the surrounding text. Also note that I've included HTML tags within the text I've written.
There is a drawback to the
document.write() method: it
only works when the document is loaded (or reloaded). Hence, you cannot
later insert text into your document (unless you reload it).
You can play with a sample page that includes this function.
Saturday, 22 January 2000
Monday, 28 February 2000
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS105/2000S/Outlines/outline.21.html
Source text last modified Mon Feb 28 09:48:45 2000.
This page generated on Mon Feb 28 09:45:12 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at email@example.com