JavaScript Examples

 

Simple javascript example

<html>
<script language="JavaScript">
<!-- hide

document.write("Welcome to my homepage!<br>");
document.write("This is JavaScript!<br>");

document.write("Welcome to my homepage!<br>");
document.write("This is JavaScript!<br>");

document.write("Welcome to my homepage!<br>");
document.write("This is JavaScript!<br>");

// -->
</script>
</html>

 

Same as above, but with a function!!

<html>
<script language="JavaScript">
<!-- hide

function myFunction() {
  document.write("Welcome to my homepage!<br>");
  document.write("This is JavaScript!<br>");
}

myFunction();
myFunction();
myFunction();

// -->
</script>
</html>

 

How to load open a new Navigator window, with options

<html>
<head>
<script language="JavaScript">
<!-- hide

function openWin2() {
  myWin= open("bla.htm", "displayWindow", 
    "width=400,height=300,status=no,toolbar=no,menubar=no");
}

// -->
</script>
</head>
<body>

<form>
<input type="button" value="Open new window" onClick="openWin2()">
</form>

</body>
</html>

 

A big page showing how to access for elements, and the onClick event

<html>
<head>
<title>Objects</title>

<script language="JavaScript">
<!-- hide

function first() {

  // creates a popup window with the
  // text which was entered into the text element

  alert("The value of the textelement is: " + 
    document.myForm.myText.value);
}

function second() {

  // this function checks the state of the checkbox

  var myString= "The checkbox is ";

  // is checkbox checked or not?
  if (document.myForm.myCheckbox.checked) myString+= "checked"
    else myString+= "not checked";

  // output string
  alert(myString);
}

// -->
</script>
</head>
<body bgcolor=lightblue>

<form name="myForm">
<input type="text" name="myText" value="bla bla bla">
<input type="button" name="button1" value="Button 1"
  onClick="first()">
<br>
<input type="checkbox" name="myCheckbox" CHECKED>
<input type="button" name="button2" value="Button 2"
  onClick="second()">
</form>

<p><br><br>

<script language="JavaScript">
<!-- hide

document.write("The background color is: ");
document.write(document.bgColor + "<br>");

document.write("The text on the second button is: ");
document.write(document.myForm.button2.value);

// -->
</script>

</body>
</html>