|
Javascript
|
| Contents |
With Javascript it is possible to have a new window opening with a specified URL input by the user.
Type in a URL (e.g. http://www.surrey.ac.uk) here to open a new window with that URL:
The Javascript code for this:
<SCRIPT language="JavaScript">
<!--
function go()
{
var theurl = window.document.theform.theurl.value;
var newwind = open(theurl,"newwind","menubar,resizeable,scrollbar");
}
-->
</SCRIPT>
The code for the form:
<form name="theform" onSubmit="go(); return false;">
<input type="text" name="theurl" size=40>
</form>
Time Functions
It is possible in Javascript to display the current time/date.
Some methods for the date object: (uses the time displayed on your computer)
getDate() returns the day of the month
getDay() Returns the day of the week
getHours() returns the hour (Starts from 0-23)!
getMinutes() returns the minutes
getSeconds() returns the seconds
getMonth() returns the month. (Starts from 0-11)!
getYear() returns the year
getTime() returns the complete time
It is also possible to create a form and have the time updated automatically every second or at any time interval you desire as shown here:
Here is the Javascript code:
<script language="JavaScript">
<!--
function showtime()
{
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()
var dn="AM"
if (hours>12)
{
dn="PM"
hours=hours-12
//this is so the hours written out is
//in 12-hour format, instead of the default //24-hour format.
}
if (hours==0)
hours=12
//this is so the hours written out
//when hours=0 (meaning 12am) is 12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
document.Tick.Clock.value=hours+":"+minutes+":"+seconds+"
"+dn
setTimeout("showtime()",1000) // update in ms
}
showtime()
-->
</script>
Here is the code:
<SCRIPT language="JavaScript">
<!--
var today_date= new Date()
var myyear=today_date.getYear()
var mymonth=today_date.getMonth()+1
var mytoday=today_date.getDate()
document.write("Today's date is: ")
document.write(mytoday+"/"+mymonth+"/"+myyear)
-->
</script>
Changing a picture dependent on the time of day
Using the document.write command which we saw earlier we can manipulate the
HTML so that dependent on the time a certain picture is loaded. Here is the
code:
<script language="JavaScript">
<!--
var current= new Date()
var day_night=current.getHours()
if (day_night<=12)
document.write("<img src='day.gif'>")
else
document.write("<img src='night.gif'>")
-->
</SCRIPT>
I have created a couple of applications to show how Javascript can be used:
A Scientific Calculator Created using JavaScript:
A JavaScript solution to the conjugate match problem:
© Nigel Martin 2000 ma71nm@surrey.ac.uk Created September 2000 Last Changed 13 April, 2001