![]() |
![]() |
|||
|
Introduction to JavaScript
Content:
Embedding JavaScript into a HTML-page
<html> <body> <br> This is a normal HTML document. <br> <script language="JavaScript"> document.write("This is JavaScript!") </script> <br> Back in HTML again. </body> </html>
There is a way for hiding the source code from older browsers. <html> <body> <br> This is a normal HTML document. <br> <script language="JavaScript"> <!-- hide from old browsers document.write("This is JavaScript!") // --> </script> <br> Back in HTML again. </body> </html> Events and event handlers are very important for JavaScript programming.
Events are mostly <form> <input type="button" value="Test this example" onClick="alert('Yo')"> </form> Other possible events are: onBlur, onChange, onDblClick, onFocus, etc. We will use functions in most of our JavaScript programs, so this is an important concept. An example: <html> <head> <script language="JavaScript"> <!-- hide function myFunction() { document.write("Welcome to my homepage!<br>"); document.write("This is JavaScript!<br>"); } // --> </script> </head> <body> myFunction(); myFunction(); myFunction(); </body> </html>Functions can also be used in combination with event-handlers. Please consider this example: <html> <head> <script language="JavaScript"> <!-- hide function calculation() { var x = 12; var y = 5; var result = x + y; alert(result); } // --> </script> </head> <body> <form> <input type="button" value="Calculate" onClick="calculation()"> </form> </body> </html>
Here you can test this example:
Object hierarchy in the HTML-document JavaScript organizes all elements on a web-page in a hierarchy. Every
element is seen as an An example:
<html> <head> <title>My homepage</title> </head> <body bgcolor=#ffffff> <img src="home.gif" name="pic1" width=200 height=100> <form name="myForm"> Name: <input type="text" name="name" value="bla bla bla"> e-Mail: <input type="text" name="email" value="xyz@bla.com"> <input type="button" value="Push me" name="myButton" onClick="alert('Yo')"> </form> <img src="ruler.gif" name="pic4" width=300 height=15> <a href="http://www.geometria.de">My homepage</a> </body> </html>Here is a screenshot of this page (some things are added with red color):
The following image illustrates the hierachy created by our example HTML-page:
Getting information about the properties of objects. The commands looks like: var nameInput = document.forms[0].elements[0].value; var imageHeight = document.images[1].height.value; Example of getting and setting object properties Look at the following example.
Source code: <html>
The following example shows, how to work with radiobuttons:
Our last example shows how to find out, which radiobutton from a list of radiobuttons is selected. <html> <head> <script language="JavaScript"> <!-- function checkRadiobuttons() { if (document.rblist.radiobutton[0].checked == true) {
Here you can test this example:
Windows and on-the-fly documents A description how to create own windows is given in Voodoo's Introduction to JavaScript. Click here to jump directly into the chapter about windows.
Exercise 1 Develop a HTML-document similar to the calculation example.
Change in the JavaScript-function var result = eval(a) + eval(b); to var result = a + b;What does happen?
Exercise 2 Develop a HTML-document similar to the calculation example. Add a list
with four radiobuttons Hint: Use the command: "if (...boolean expression ...) { ... some code ... } else { ... some code ... }"
Exercise 3 Choose a Geometria sketch and add a button to the HTML-document.
|
|||