CpSc 130 Notes Javascript and Dynamic Web Pages
It is traditional in programming languages to first write a program that simple says "Hello world" when it is run. Part of the reason for this is to (hopefully) succeed at making the program do what we want. (That's a bit like making HTML format a page the way you want.) The other reason is to see how difficult or easy it is.
Of course, we can make a web page that says "Hello world" using HTML easily, but our goal is to use JS to make a web page say it without simply coding it in HTML.
The code shown has nothing in the body. It is a file with no real HTML content. Everything we need to do is provided in the body tag itself.
The onLoad attribute specifies that when the event page-is-finished-loading occurs, the JS interpreter should interpret the code provided as its value: alert("Hello world!"); The alert() function expects to be given a string of text to show the user. Note that the text is provided as a "string" inside the function's parenthesis. The text is called an argument (or a parameter). They are used to pass information to a function. |
hello1<html> <head> <title>Hello World</title> </head> <body onLoad='alert("Hello world!");'> </body> </html> |
Since the onLoad string value is given in single-quotes, there can be no single-quotes in the enclosed JS. Usually, this presents no problems since we have been using double-quotes most of the semester, but watch for apostrophes! Also, notice the semi-colon. It serves as an end of statement marker. Automatic semi-colon insertion refers to a cool trick used by some JS programmers. When a browser notices that you appear to have ended a JS statement by simply going to the next line, it inserts a semi-colon for you. Very bad practice. We don't want the browser deciding for us. Always use semi-colons. |
|
Here I use the onClick event with two different elements. <strong> is an inline element that can contain any amount of text. The <h2> is a block element. Notice that using onClick does not make an image or text show as a hyperlink (anchor), because it is not. The cursor doesn't even change to indicate the text is clickable (unless we tell it to). |
hello2<body> <h1>Sample onClick web page</h1> <p>To see results <strong onclick="alert('Hello world');"> click me</strong> now.</p> <h2 onclick="alert('Now we are having fun');"> Click me too!</h2> </body> |
Note that the roles of single- and double-quotes are reversed here. Also, try clicking anywhere on the line of the block element, as opposed to the inline element. |
The example in Figure 4.1 uses onmouseover and onmouseout and these operate as you would expect.
<input type="button" value="Do it!" onclick="alert('Hello')">
<head> <title>Mystery Image </title> </head> <body> <img id="myImage" src="redball.gif"> <input type="button" value="Do it!" onclick="document.getElementById('myImage').src='bullet.gif'"> </body> |
Try it. Use these images: and and type in the code to the left. It will look like: |
Let's decypher the above example.
document.getElementById() looks up an element in the current page that has the id specified as its argument ("myImage").
The .src (dot-src) specifies that we are interested in the src attribute of the element we just looked up. Note that there are no spaces – this is one long variable name.
We know that the src attribute of the <img> element gives the name of a file to display. What happens if you change it? The new image is displayed!
<body> Welcome to my page <input type="button" value="Do it!" onclick="document.getElementById('myText').innerHTML='I changed the text!'"> <div id="myText"> There is nothing here of interest </div> </body> |
Welcome to my page
There is nothing here of interest
|
Let's decypher the above example.