document.write and if
Hello |
var numhours = 11; alert ("hello"); if (numhours >=12) { document.write("Good Afternoon"); } else { document.write ("Good Morning"); } |
timeout and document.bgColor
Example Using timeout |
document.write ("hello"); setTimeout( "document.bgColor =\"blue\"", 1500); setTimeout( "document.bgColor =\"red\"", 2000); setTimeout( "document.bgColor =\"green\"", 2500); setTimeout( "document.bgColor =\"orange\"", 3000); |
Window.prompt and parseInt
prompt and adding |
var firstNumber, // first string entered by user secondNumber, // second string entered by user number1, // first number to add number2, // second number to add sum; // sum of number1 and number2 // read in first number from user as a string firstNumber = window.prompt( "Enter first integer", "0" ); // read in second number from user as a string secondNumber = window.prompt( "Enter second integer", "0" ); // convert numbers from strings to integers number1 = parseInt( firstNumber ); number2 = parseInt( secondNumber ); // add the numbers sum = number1 + number2; // display the results document.writeln( "<H1>The sum is " + sum + "</H1>" ); |
date object, if, alert, string methods
Example of Date for IE |
var dateandtime; //hold the date and the time var hours; //just the hours var today; // just the date dateandtime=new Date(); // built-in date object hours=dateandtime.getHours(); // built-in getHours method today = dateandtime.toLocaleString(); // convert to extract today = today.substring(0,10); // extract just the date alert ("Today's date is "+today+""); document.write("Welcome <br>") if (hours >=12) { document.write("Good Afternoon"); }else{ document.write("Good Morning"); } |
Date methods
Example of Date for any Browser |
// This program is a sample of extracting the date for any browser // Dr. Deborah Whitfield // Cpsc130 var dateandtime; //hold the date and the time var hours; //just the hours var day; // just the day var month; // jsut the month var year // the year dateandtime=new Date(); // built-in date object hours=dateandtime.getHours(); // built-in getHours method month = dateandtime.getMonth()+ 1; // built-in getMonth method day = dateandtime.getDate(); // built-in getDate method year = dateandtime.getFullYear(); // built-in getYear method alert ("Today's date is "+ month + '/' + day + '/' + year); document.write("Welcome <br>") if (hours >=12) { document.write("Good Afternoon"); }else{ document.write("Good Morning"); } |
Timeout, date methods, string methods
Example timeout and date |
var dateandtime; //hold the date and the time var hours; //just the hours var today; // just the date dateandtime=new Date(); // built-in date object hours=dateandtime.getHours(); // built-in getHours method setTimeout("document.bgColor = \"red\"", 500); setTimeout("document.bgColor= \"white\"", 1000); setTimeout("document.bgColor = \"blue\"", 1500); setTimeout("document.bgColor = \"white\"",2000); setTimeout("document.bgColor = \"green\"", 2500); setTimeout("document.bgColor = \"white\"", 3000); setTimeout("document.bgColor = \"blanchedalmond\"", 3500); today = dateandtime.toLocaleString(); // convert to extract today = today.substring(0,10); // extract just the date alert ("Today's date is "+today+""); document.write("Welcome <br>") if (hours >=12) { document.write("Good Afternoon"); }else{ document.write("Good Morning"); } |
if statement
Comparison and tables |
var num1, num2; num1 = window.prompt ("Enter 1st number", "0"); num2 = window.prompt ("Enter 2nd number", "0"); document.writeln ("<TABLE border='1' width = '100%'>"); if ( num1 == num2 ) document.write ("<tr><td>They are equal <td><tr>"); else document.write ("<tr><td>not equal <td><tr>"); if (num1 < num2) document.write ("<tr><td>num1 smaller <td><tr>"); if (num1 > num2) document.write ("<tr><td>num1 bigger <td><tr>"); if (num1 < num2) document.write ("<tr><td>num1 less than or equal <td><tr>"); if (num1 >= num2) document.write ("<tr><td>num1 greater than or equal <td><tr>"); document.writeln ("<Table>"); |