Jeff's Javascript Examples


Local Clock Example

In Action...

The Code...

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var timerID = null;
var timerRunning = false;

function stopclock (){
  if(timerRunning)
    clearTimeout(timerID);
  timerRunning = false;
  }

function showtime () {
  var now = new Date();
  var hours = now.getHours();
  var minutes = now.getMinutes();
  var seconds = now.getSeconds()
  var timeValue = "" + ((hours >12) ? hours -12 :hours)
  timeValue += ((minutes < 10) ? ":0" : ":") + minutes
  timeValue += ((seconds < 10) ? ":0" : ":") + seconds
  document.clock.face.value = timeValue;
  timerID = setTimeout("showtime()",1000);
  timerRunning = true;
  }

function startclock() {
  stopclock();
  showtime();
  }

// End -->
</SCRIPT>

...

<FORM name="clock">
<input type="reset" name="face" value="XX:XX:XX">
</FORM>

Last Modified Example

In Action...

The Code...

<SCRIPT>
<!--
onLoad=document.write("Last Modified: " + document.lastModified);
// -->
</SCRIPT>

Automatic Go Back Example

In Action...

The Code...

<SCRIPT>
<!--
onLoad=document.write('<A HREF="' + document.referrer + '">Go BACK</A>');
// -->
</SCRIPT>

Fastest MouseOver Implentation

In Action

Babybook

The Code...

<SCRIPT>
  <!--
    image1=new Image();image1.src="mon.gif";
 //-->
</SCRIPT>
...
<A HREF="babybook"
  onMouseOver="document.images[0].src='mon.gif'"
  onMouseOut="document.images[0].src='moff.gif'"><IMG SRC="moff.gif" ALT="Babybook" BORDER=0></A>

Browser Identification

In Action...

The Code

<SCRIPT>
  <!--
    var ns = false;
    var msie_four = false;
    var browser = navigator.userAgent.substring(0,9);
    var ms = navigator.userAgent.substring(25,31);
    if (ms == "MSIE 4") msie_four = true;
    if (browser == "Mozilla/3" || browser == "Mozilla/4") ns=true
      ...
    if (ns || msie_four)
    {
      onLoad=document.write("you are using a netscape 3.x or higher compatible browser");
      } else {
      document.write("you are not using a netscape 3.x or higher compatible browser");
    }
  //-->
</SCRIPT>

Form Entry Alert Example

In Action

The Code

<script language="JavaScript">
<!-- Hide script
function isEmpty(exp)
{
  if (exp == null || exp == "")
  {
    alert ("You did not enter any information");
    document.forms[1].This_Field.focus();
    return false
  }
  return false;
}
// -->

</script>

...

<FORM ACTION="null.cgi" METHOD="GET"
onSubmit="return isEmpty(document.forms[0].This_Field.value)"
><INPUT TYPE="text" NAME="This_Field">
<P><INPUT TYPE="submit">
</FORM>

This is some standard HTML, unlike the above that is generated.

Enter an expression:
Result: