DHTMLexercises
DHTMLexercises
DHTMLexercises
Type the following scripts into an editor. I suggest you use PFE rather than Notepad.
Save them as htm files. View them. Understand what they do.
Exercise 1
Exercise 3
Exercise 5
<script language="JavaScript">
function AlignMe(a) {
if (!document.getElementById) return;
h=document.getElementById("head1");
h.setAttribute("align",a);
}
</script> </head>
<body>
</body> </html>
Exercise 6
<script language="JavaScript">
function DisplayEvent(e) {
span=document.getElementById("addhere");
logentry= e.type;
if (e.type=="keypress") {
if (e.keyCode) keycode=e.keyCode;
else keycode=e.which;
key=String.fromCharCode(keycode);
logentry += " key=" + key;
}
if (e.type=="mousedown"||e.type=="mouseup"||e.type=="click") {
if (e.button) button=e.button;
else button=e.which;
logentry += " button=" + button;
}
txt=document.createTextNode(logentry);
span.appendChild(txt);
span.appendChild(document.createElement("BR"));
}
</script> </head>
<body onKeyPress="DisplayEvent(event);">
<b>Event Log:</b><hr>
<span ID="addhere"></span>
</body> </html>
Exercise 7
Exercise 8
<script language="javascript">
var pos=0;
var direction=1;
function Move() {
obj=document.getElementById("head1");
pos += direction;
if (pos >= 100 || pos <= -100) direction = 0 - direction;
obj.style.left=pos;
window.setTimeout("Move();",25);
}
</script> </head>
<body onLoad="Move();">
Exercise 9
<script language="javascript">
msg = "This is an example of a scrolling message. ";
msg += "Notice that the actual message is larger ";
msg += "and only a portion is displayed at once. ";
pos = 0;
function ScrollMessage() {
newtext = msg.substring(pos, msg.length) + "... ..." + msg.substring(0, pos);
newtext=newtext.substring(0,80);
obj = document.getElementById("scroll");
obj.firstChild.nodeValue = newtext;
pos++;
if (pos > msg.length) pos = 0;
window.setTimeout("ScrollMessage()",100);
}
</script> </head>
<body onLoad="ScrollMessage();">
<p>The text below is scrolled across the screen using DHTML. This allows text to be
scrolled directly in the body of the page rather than within a form or the status line.</p>
<hr>
<pre ID="scroll">This text is required, but will be replaced.</pre>
<hr>
</body> </html>
Template
function (){
}
</script> </head>
<body>
<h1> </h1>
<p>
</p>
</body> </html>