V ' I J S: Part 2: The HTML-document
V ' I J S: Part 2: The HTML-document
<html>
<head>
</head>
<body bgcolor=#ffffff>
<center>
<img src="home.gif" name="pic1" width=200 height=100>
</center>
<p>
<form name="myForm">
Name:
<input type="text" name="name" value=""><br>
e-Mail:
<input type="text" name="email" value=""><br><br>
<input type="button" value="Push me" name="myButton" onClick="alert('Yo')">
</form>
<p>
<center>
<img src="ruler.gif" name="pic4" width=300 height=15>
<p>
</body>
</html>
document.forms[0].elements[0]
But how can we now get to know the entered text? In order to find out which properties and
methods an object offers you have to look into a JavaScript reference (for example Netscape’s
documentation or the reference in my book). There you will see that a textelement has got the
property value. This is the text entered into the textelement. Now we can read out the value with
this line of code:
name= document.forms[0].elements[0].value;
The string is stored in the variable name. We can now work with this variable. For example we
can create a popup window with alert("Hi " + name). If the input is ’Stefan’ the command
alert("Hi " + name) will open a popup window with the text ’Hi Stefan’.
If you have large pages it might get quite confusing by addressing the different objects with
numbers - for example document.forms[3].elements[17] or was it document.forms[2].ele-
ments[18]? To avoid this problem you can give unique names to the different objects. You can
see in our HTML-code that we wrote for example:
<form name="myForm">
Name:
<input type="text" name="name" value=""><br>
...
This means that forms[0] is also called myForm. Instead of elements[0] you can use name (as
specified with the name-property in the <input> tag). So instead of writing
name= document.forms[0].elements[0].value;
name= document.myForm.name.value;
This makes it much easier - especially with large pages with many objects. (Please note that you
have to keep the same case - this means you cannot write myform instead of myForm) Many
properties of JavaScript-objects are not restricted to read-operations. You can assign new values
to these properties. For example you can write a new string to a textelement.
Here is the code for this example - the interesting part is inside the onClick-property of the se-
cond <input> tag:
<form name="myForm">
<input type="text" name="input" value="bla bla bla">
<input type="button" value="Write"
onClick="document.myForm.input.value= ’Yo!’; ">
I cannot describe every detail here. It gets much clearer if you try to understand the object hier-
archy with the help of a JavaScript reference. I have written a small example. There you will see
the use of different objects. Try to understand the script with the help of Netscape’s documen-
tation - or better: with my JS-book... :-)
<html>
<head>
<title>Objects</title>
<script language="JavaScript">
<!-- hide
function first() {
function second() {
// output string
alert(myString);
}
// -->
</script>
</head>
<body bgcolor=lightblue>
<form name="myForm">
<input type="text" name="myText" value="bla bla bla">
<input type="button" name="button1" value="Button 1"
onClick="first()">
<br>
<input type="checkbox" name="myCheckbox" CHECKED>
<input type="button" name="button2" value="Button 2"
onClick="second()">
</form>
<p><br><br>
<script language="JavaScript">
<!-- hide
// -->
</script>
</body>
</html>
The location-object
Besides the window- and document-objects there is another important object: the location-ob-
ject. This object represents the address of the loaded HTML-document. So if you loaded the
page http://www.xyz.com/page.html then location.href is equal to this address. What is more im-
portant is that you can assign new values to location.href. This button for example loads a new
page into the actual window:
<form>
<input type=button value="Yahoo"
onClick="location.href=’http://www.yahoo.com’; ">
</form>