Most beginning web developers would rather avoid Javascript altogether, given
the choice. Javascript can be a tricky, tempermental language to work with.
It looks stranger than HTML or CSS, and it is not nearly so easy to divine what
a line a Javascript is doing.
So why would we use such a thing? Unfortunately, Javascript is the only
way to accomplish certain tasks. In general, Javascript is used whenever we
need to process logic on the client side. In other words, if we are to do any
true programming which will run on the user's browser, Javascript is our only
option.
Remember that client side processing is a lot different than server side processing.
The majority of your tasks may well be better suited for the webserver. But
features relating specifically to the user interface live in Javascript's domain.
Place Javascript functions (more on these below) in the HTML header of your document. Tell the browser it's Javascript with the <script> tag. It is also considered best practice to enclose the script itself as an HTML comment, as some less intelligent browsers apparently print the script onscreen (duh).
<head>
<script language="Javascript">
<!--
[Javascript code here]
//-->
</script>
</head>
The other way to load in Javascript with a webpage is by creating an external
file. Just as with CSS files, Javascript files (with the extension .js) can
be handy to use for the purpose of loading them to multiple webpages. No need
to repeat all that code in each page's HTML header.
With Javascript, simply give the "src" attribute a value in the script
element. It is reminiscint of how we load images:
<head>
<script language="Javascript" src="factory.js">
</script>
</head>
function openWin() {
myWin= open("day.html", "popup",
"width=520,height=370,scrollbars=yes,status=yes,toolbar=no,menu=no");
}
A semicolon defines a statement in Javascript. There is only one statement in the openWin function. If there had been more, each would have executed to completion before the next would begin. This means that Javascript, unlike HTML or CSS, is a procedural language. That's a fancy way of saying you can write logic in a step-by-step manner to accomplish more complex tasks.
<script language="Javascript">
<!--
function myFunction() {
[statement 1];
[statement 2];
[statement 3];
[statement 4];
}
//-->
</script>
<body onLoad="openWin()">
The function will run upon page load.<a href="anotherpage.html" onClick="openWin()">click here</a>
This will not only load anotherpage.html, but it will run the openWin function, loading day.html into a new window.Triggering Javascript functions whenever a user mouses over a link can get chaotic, but it can be useful for some things. The most familiar is the rollover button. For any usage, when a user mouses over the linked area, the onMouseover action is invoked. The onMouseover link also goes in the <a> tag:
<a href="anotherpage.html" onMouseover="someFunction()"><img src="click_here.gif"></a>
In this case we are linking an image to the onMouseover event.
<a href="anotherpage.html" onMouseout="someFunction()">click here</a>
onSubmit is an action keyword used in the <form> tag, since the only things we submit within HTML are forms. Often this is used to run form validation functions, to check that the data the user has entered in the form is of proper character:
<form action="../cgi-bin/register.cgi" method="post"
onSubmit="checkForm()">
[input fields]
</form>
Indeed the path of the web designer soon turns down the road of development;
you are no longer content to let the robots write your code. The truth is that
many attractive and functional elements of website design are somewhat tricky
to implement, and require your full attention to customize at the code level.
In computing, everything has a name. Therefore if you want to gain control over
it, you need it to provide it a name and call it by that name. Assign a value
to the name attribute in your targeted form:
<form name="form1" method="post" action="../cgi-bin/mailinglist.cgi">
[input fields]
</form>
<a href="javascript:document.forms['form1'].submit();">[text or an image]</a>
This <a> tag will link the submit action to whatever text or images you wrap in it.Sometimes Javascript can be inserted directly into the body of your HTML, surrounded by <script> tags of course. Setting a "last modified" trigger is an example of doing this. The below code will actually "ask" the file when it was last changed and write the result to the screen:
<script language="Javascript">
var modifieddate=document.lastModified
document.write(modifieddate)
</script>
-rw-r--r-- 1 mindmine mindmine 6576 Dec 1 19:50 index.html
Netscape gives us the file's timestamp in Greenwich Mean Time (GMT) while Internet Explorer took the file's timestamp on my webserver (which is an hour behind me in the Midwest) and translated it to my time zone, an hour later. The onscreen result in both cases was different than the actual timestamp, but both deadly accurate in their own way.As we've discussed, Javascript gives us access to some advanced user interface
features available to the browser. One such feature is the Javascript alert
box. The alert is a standard pop-up dialogue box that requires the user to click
"OK" before proceeding. Click this link for an example if you are
reading this online (it will look a bit different in each browser).
The alert is constructed as a function up in the HTML header, and waits for
an event to trigger it:
<script language="Javascript">
<!--
function myAlert () {
alert("Please read the agreement.");
}
//-->
</script>
Did you read the licensing agreement?<br><input type="radio"
name="agreement" value="yes">yes
<input type="radio" name="agreement" value="no"
onClick="myAlert()">no
Close cousin to the alert dialogue is the Javascript confirm. Dialogue is a
more fitting term when talking about "confirm", as confirm gives the
user an opportunity to cancel before proceeding, as opposed to alert's decisive
"OK".
Write a confirm function to capture the result of the user's choice. The way
to capture a result is to assign a variable to it. We'll call our variable "result".
We'll call our function "confirmBox". Depending on the value of "result"
(confirm returns "true" for "OK" and "false" for
"Cancel") we'll return the appropriate value to the method that called
us, probably onSubmit, using an if-else statement.
<script language="Javascript">
<!--
function comfirmBox() {
var result = confirm("Proceed?");
if (result == true) {
return true;
}
else {
return false;
}
}
//-->
</script>
<form method="post" action="" onSubmit="return confirmBox()">
<input type="submit" value="Go">
</form>
Focus is a term for where the browser's attention is at a given moment. This
is most often applied to input fields, since we often wish to prompt a user
proactively with a blinking cursor in the field we want noticed. Javascript
offers the focus() method for this purpose.
Let's first create a form with various fields in which we might want to establish
focus. We'll be sure to name the form, and the fields to of course, as we will
be calling them by name in Javascript.
<form name="myCoolForm" method="post" action="">
first name: <input type="text" name="first_name"><br>
last name: <input type="text" name="last_name"><br>
occupation: <input type="text" name="occupation"><br>
age: <input type="text" name="age">
</form>
<script language="Javascript">
function putFocus() {
myCoolForm.first_name.focus();
}
</script>
<a href="#" onClick="putFocus()">click here</a>
More often, we may want to apply the focus upon page load:<body onLoad="putFocus()">
Focus can also be very effective when used along with alert. Alert the user as to the error, and focus on the field that needs correcting.The status bar is found at the bottom left of most browsers. It shows by default
but keep in mind: it can be turned off by the user in most cases. However, the
majority of people don't do this, so writing to the status bar is plenty worth
it.
This very webpage has status bar text ("The Webslave's Handbook");
this is how it was done. Just a bit of Javascript in the header, invoking the
onload event through the window object. This really isn't any different than
using <body> onLoad.
<script language="Javascript">
window.onload=new Function('window.status="The Webslave\'s Handbook"')
</script>
Dropdown menus are sometimes useful to present a choice of links to other pages. For this, prepare a function as below, naming the elements you'll need to control.
<script language="Javascript">
function goTo() {
onClick =
window.location =
document.linkForm.linker.options[document.linkForm.linker.selectedIndex].value
}
</script>
<form name="linkForm">
<select class="type1" name="linker"
onChange="goTo()">
<option SELECTED>jump to...
<option value="html.html">HTML section
<option value="css.html">CSS section
<option value="perl.html">Perl section
</select>
Sound event handling has three components. First and foremost you need to "embed" the audio in the page, so it is loaded with everything else up front. The <embed> tag has a couple of notable options, including the ability to autostart the sound on page load. If you want the sound to play immediately, you need nothing else than the below code somewhere in the body of the HTML:
<embed src="sounds/cheer.wav" autostart=true hidden=true name="cheer">