Lesson 8 - JAVA Scripts

JAVA SCRIPTS

    Compared to JAVA applets, JAVA scripts are much more easier to use and edit. There won't be any '.class' files needed. All you have to do is just copy down every single line of code to make sure your script would work. Here's a simple string of code that shows when your page was last updated. Just copy and place it within the <body> tag :

<script>
<!--//hide
document.write(document.lastModified);
//end hide-->
</script>

    Here's another script which would allow a pop-up window to appear whenever someone visits your homepage and whenever someone leaves it. It should be inserted inside the <body> tag :

<body onLoad="window.alert('Welcome!');"
onUnLoad="window.alert('Bye! See you again!');">

    This next script allows a line of text to scroll past your status bar. As for the scrolling speed, '200' represents 0.2 seconds. Just copy and insert the code between the <head></head> tags :

<script language="JavaScript">
<!--
scrtxt="Row, row, row your boat!"
txtlength=scrtxt.length
pos=width=40
if(navigator.appName=="Netscape") step=2
else step=1

function scrolling()
{
var statustxt=""
if (pos+txtlength<0) pos=width
if (pos>=0) {
for (var i=1;i<=pos;i++)
statustxt=statustxt+"   "//
statustxt=statustxt+scrtxt;
}
else
statustxt=scrtxt.substring(-(pos),txtlength)
window.status=statustxt
pos=pos-step
setTimeout("scrolling()",200)
}
-->
</script>


    Add another line to the <body> tag, like this :

<body onLoad='scrolling()'>

    Try this one! This script lets you explain what each link on your homepage is all about. The explanation would appear on the status bar of your browser. It is in 2 parts, the first part should be inserted between the <head> tag :

<script language='JavaScript'>
<!--
function clearmsg()
{
window.status=''
return true
}
-->
</script>


    This part should be inside the <a href> tag :

<a href="me.html" OnMouseOver=
'window.status="All about me"; return true;'
OnMouseOut="clearmsg()">Me</a>

    The next 2 codes lets you show the current time and date on your homepage. Try them out! Just copy them and paste it within the <head> tag :

<script language="JavaScript">
<!--
function showclock()
{
today=new Date();
document.clockform.AllArea.value=today
}
-->
</script>


    Here's what it should be in the <body> tag :

<body onLoad='showclock()'>
<form name=clockform>
This is the time :
<input type=text name=AllArea size=30>
</form>


    This one just shows the time, it's also in 2 parts :

<script language="JavaScript">
<!--
function showclock()
{
today=new Date();
Timetxt=today.getHours()+':'+today.getMinutes()+':'
+((today.getSeconds()<10)?('0'+today.getSeconds()):today.getSeconds())
document.clockform.TimeArea.value=Timetxt
setTimeout("showclock()",1000)
}
-->
</script>


    This should go inside the <body> tag :

<body onLoad='showclock()'>
<form name=clockform>
This is the time :
<input type=text name=TimeArea size=10>
</form>

    Click here to get to Lesson 9 : Cascading Style Sheets.


This Page Is Published And Maintained By Wong Kuan Yew
Copyright © 2000 Wong Kuan Yew. All Rights Reserved.
Last Updated: