Page 3 of 6
<![if !supportEmptyParas]> <![endif]>
function processLink (form) {
if (confirm("Do you want to link to the sound file (145K)?"));
location = "http://mydomain.com/somesound.au/";
}
The confirm box displays a question with Yes and No buttons. If the answer is Yes, the sound is loaded. If the answer is no, the location instruction is skipped and the sound is not loaded. Another method for "pre-processing" a link uses links that are defined with the same HREF and NAME. This technique allows the anchor to link to itself. The advantage of this method is that you can avoid the display of the JavaScript URL.
<![if !supportEmptyParas]> <![endif]>
<A HREF="#1" NAME="1" onClick="processLink()">
SUBHEAD: Display a link for JavaScript users only By putting the text for a link in JavaScript code, you can display a link for only those users with JavaScript. The technique works like this:
<![if !supportEmptyParas]> <![endif]>
<SCRIPT>
<!-- hide from other browsers
document.write ('<A HREF="myurl.html">Click here, JavaScripters! </A>')
//-->
</SCRIPT>
Only those browsers with JavaScript will render the hypertext link. This feature gives you the advantage of custom-designing
your pages so that those without JavaScript aren't even aware of the JavaScript-only features of your page. SUBHEAD: Use string
methods with the window.location object The window.location object returns the URL of the current document in the window. (Note: window.location is the same as just plain location, assuming you're referring to the current window; that's exactly what we'll assume for the remainder of this section). You
can see this URL string with some simple code:
<![if !supportEmptyParas]> <![endif]>
Ret = location;
alert (Ret);
Often, you'll want to manipulate the string returned by the location object. But if you try something like this
<![if !supportEmptyParas]> <![endif]>
Ret = location;
LastFour = Ret.substring (Ret.length-4, Ret.length);
alert (LastFour);
you'll get an error ("length is not a member"). You'll get different errors depending on what string methods you try to use.
Remember that location is an object, even though it seems to return a string value -- in fact, a value you can readily see using the alert box.
In order to manipulate this value with string methods, you'll need to convert it to a real string. One way to accomplish this
conversion is to append the toString function to the end of the location object. Using this approach, the previous script will work just fine, as shown here:
<![if !supportEmptyParas]> <![endif]>
Ret = location.toString();
LastFour = Ret.substring (Ret.length-4, Ret.length);
alert (LastFour);
Another approach is to use the href property of the location object, as in:
<![if !supportEmptyParas]> <![endif]>
Ret = location.href;
And still another method is to use the location property of the document object, which (depending on circumstances) returns the same URL. The syntax is:
<![if !supportEmptyParas]> <![endif]>
Ret = document.location
SUBHEAD: Display a clock on a page A fairly common application for JavaScript is displaying a clock on the page. It's easy to do and mildly useful. I know of a number of different ways you can implement a clock, several are a bit too high-tech and involved for these virtual pages, but one solution will work just fine. This lower-tech technique for displaying the current time uses a form text box to keep the time updated. I wrote the following script so it would be fairly easy to modify. For example, you can readily alter it so it returns 24-hour time rather than 12-hour time, and you can change the period of updating from once every 30 seconds to a longer or shorter interval.
·
·
·
·