|
typewriting message
This method comes in handy when you want to type into a textarea box and have formatted text appear in any given place within the document. I’ve seen this used as the base of very complex and capable web applications that are used to develop the look of a document by adding CSS rules to the text that is being displayed.
<html>
<head>
<script type="text/javascript">
message="The best way to learn is to study examples ";
pos=0;
maxlength=message.length+1;
function writemsg() {
if (pos<maxlength) {
txt=message.substring(pos,0);
document.forms[0].msgfield.value=txt;
pos++;
timer=setTimeout("writemsg()",50);
}
}
function stoptimer() {
clearTimeout(timer);
}
</script>
</head>
<body onload="writemsg()" onunload="stoptimer()">
<form>
<input id="msgfield" size="65">
</form>
</body>
</html>
We used a nested if loop to test if the text being typed is to be displayed. Note that we used a timer to give a little bit of a lag between the time the key is pressed and the time that the character is displayed. This is so the rest of the functions of the browser – which follows a bubbling or much the same as the documents it displays – can have sufficient time to apply the formatting to the text it has been told to display. An onLoad event handler is used to begin the function as the page has completed loading.
|
|