These Demos are based, in part, on material from Felke-Morris's Web Development & Design Foundations with XHTML
Example 1
Let's get started.
That's enough for now.
Example 2
document.write(" 'document.write' Demo
");
document.write(" This example show we can include XHTML tags.
");
document.write(" We can also call functions.
");
document.write(document.lastModified);
Example 3
Mouseover Demo
Mouseout Demo
Example 4
var userName1, userName2;
userName1 = "Joe";
userName2 = prompt("Please enter your name: ");
document.write(" Hello " + userName2 + ". Welcome to my class. --- "
+ userName1 + "");
Example 5
var userName, userColor;
userName = prompt("Please enter your name: ");
document.write(" Hello " + userName + ". ");
userColor = prompt("Hello " + userName + ". What is your favorite color? ");
document.bgColor = userColor;
Example 6
var userName, userColor;
userName = prompt("Please enter your name: ");
document.write(" Hello " + userName + ". ");
userColor = prompt("Hello " + userName + ". What is your favorite color? ");
if (userColor == "green")
{
document.write("Green is my favorite color too!");
} else
{
document.write(userColor +
" is a nice color, but it isn't my favorite.");
}
Example 7
function showAlerts()
{
alert("Click OK for a funny joke.");
alert("Click OK to continue.");
alert("Are you sure?");
alert("Are you really sure?");
alert("Okay. All your files have been deleted.");
}
showAlerts();
Example 8
var direction, inValue, outValue;
direction = prompt("Enter an 'F' or 'C' (without the quotes).");
if (direction == 'F' || direction == 'f')
{ inValue = prompt("Enter the temperature in Celsius: ");
outValue = 9.0/5.0 * inValue + 32
document.write("The equivalent Fahrenheit temperature is: ", outValue);
}
if (direction == 'C' || direction == 'c')
{ inValue = prompt("Enter the temperature in Fahrenheit: ");
outValue = 5.0/9.0 * (inValue - 32.0)
document.write("The equivalent Celsius temperature is: ", outValue);
}
Example 9
// This is a comment. It is ignored by the computer
// Comments are used to describe code to people that might be looking at it.
// First, we want to set aside some spots in memory to hold information.
// We'll call those storage areas "firstNumber", "secondNumber" and "sum"
var firstNumber, secondNumber, sum;
// Next we'll ask the user to supply us with two of those numbers.
// Notice that we are putting the values in the storage areas we created
// in the last step.
firstNumber = prompt("Enter a number: ");
secondNumber = prompt("Enter a second number: ");
// Now we will do some math. The "Number()" business makes sure
// the computer treats the entered values as numbers and not text.
sum = Number(firstNumber) + Number(secondNumber);
// Finally, we'll write out or display a label ("The sum is: ") and
// the answer (sum).
document.write("The sum is: " + sum);