Basic JavaScript Commands & Event Handlers
These commands are usually used within <Script> tags
To use or test commands: copy and paste the code inside <script> tags in the body of a demo page.
alert Message box with [OK] only button. Ex: Use FTP to upload your webpage.
Code: <a href="javascript: alert ('FTP is File Transfer Protocol');">FTP</a>
confirm Message box with [OK]/[Cancel]. Ex: var tax = confirm ("Taxable?");
Full Code: <a href="javascript: var tax = confirm('Taxable?');">confirm</a>
prompt Prompted text box. Ex: var state = prompt ("Enter State:", "FL");
Code: <a href="javascript: var state = prompt('Enter State:', 'FL');">prompt</a>

document.write

Note:
html tags are
in quotes.

<script>
document.write("Hello World" + "<br>");
var firstName = "Cindy";
document.write("Name= " + firstName + ". Length of name= " + firstName.length + "<br>");
var amountDue = 5.678;
document.write("Amount= " + amountDue + " Amount Rounded= " + amountDue.toFixed(2) +"<br>");
document.write(new Date + "<br>");
document.write(navigator.appVersion + "<br>");
document.fgColor = "Blue"; // This is why the whole document foreground text is blue
</script>

  = = = = = Below are the RESULTS from the above code: (Do not copy the results) = = = = =

var Define (and assign a value) for a variable (optional) Ex: var taxRate = .07;
if / else var taxable = true
if (!taxable) {taxRate = 0;} else {taxRate = .07;} document.write(taxRate);
new Create a new object. Ex: var weekdays = new Array("Mon", "Tue", "Wed", "Thu", "Fri");    See below
for For Loop. This loop will display the weekdays in a column.
var weekdays = new Array("Mon", "Tue", "Wed", "Thu", "Fri");
for (i in weekdays) {document.write(weekdays[i] + "<br>"); }
or
for (i=1; i <=10; i++) {document.write([i] + " "); } // count to 10. (start, end, increment)
do while Do While Loop. This loop will make user enter hours until a valid number is entered.
do {var hours = prompt("Enter hours (1-60)", 40);}
while (hours <=0 || hours > 60);
Math.round Round. Ex: alert(Math.round(1.6666)); // result is 2
window.open <a href="javascript: var newWin = window.open('popup.htm','popUp','height=400, width=600');">PopUp</a>
PopUp
(This command will vary from browser to browser and also depends on browser settings.)
window.close <a href="javascript:window.close();">Close Window</a>
<!-- the above line is used in popup.htm -->
function Copy and paste in <head>:
<script language="javascript" type="text/javascript">
function tax(salesAmount) {
salesTax = salesAmount * .07;
salesTax = salesTax.toFixed(2);
return salesTax;}
</script>

Copy and paste in <body>:
<script language="javascript" type="text/javascript">
salesAmount = prompt("Enter salesAmount","");
salesTax = tax(salesAmount);
document.write("Amount: $" + salesAmount + " Tax: $" + salesTax);
</script>
 
Loading an External JavaScript file
In <head> <script src="scripts/daily_message.js" type="text/javascript">
</script>
In <body> <a href="javascript:dailyMessage();">Click here</a>
 
These are examples of Event Handlers:
onChange, onError, OnLoad, OnKeyPress, onBlur, OnSubmit...
These commands are NOT used within <Script> tags
onLoad <body onLoad="MM_preloadImages('0ClassFolders/2820Web/Images/HomeOn.gif')">
a href=" <a href="javascript: alert ('Date/Time is: ' + new Date());">Show date/time</a>
onClick <a href="#info" onClick = "alert('More Info \nAnd More');">Click for more info</a>
onMouseOver <a href="#" onMouseOver= "alert('This is a MouseOver demo')">Hover Here</a>