| Webmaster SIG Tools & Tips
May 20,
2004
JavaScript
Script Tag
<script
language="JavaScript">
<!-- Hide from non-JavaScript browsers
Script commands and comments
//
Stop hiding from older browsers -->
</script>
Sending Output to
a Web Page
<script
language="JavaScript">
<!-- Hide from non-JavaScript browsers
document.write("Today is May 20, 2004<br>")
document.write("Only 180 days until Christmas")
//
Stop hiding from older browsers -->
</script>
Example 1
Working with
Variables and Data
Declaring
Variables
var variable;
var variable = value;
variable = value;
<script
language="JavaScript">
<!-- Hide from non-JavaScript browsers
var Today;
var ThisDay;
var ThisMonth;
var ThisYear;
var DaysLeft;
document.write("Today is May 20, 2004<br>")
document.write("Only 180 days until Christmas")
//
Stop hiding from older browsers -->
</script>
Working with
Dates
Variable = new
Date("month, day, year, hours:minute:seconds
Variable = new Date(year,
month, day, hours, minutes, seconds)
<script
language="JavaScript">
<!-- Hide from non-JavaScript browsers
var Today=new Date("May 20, 2004);
var ThisDay;
var ThisMonth;
var ThisYear;
var DaysLeft;
document.write("Today is May 20, 2004<br>");
document.write("Only 180 days until Christmas");
//
Stop hiding from older browsers -->
</script>
Retrieving Values
| Today.getSeconds() |
Retrieves the seconds form
the date |
| Today.getMinutes() |
Retrieves the minutes from
the date |
| Today.getHours() |
Retrieves the hours from
the date |
| Today.getDate() |
Retrieves the day of the
month |
| Today.getDay |
Retrieves the day of the
week
(0 = Sunday, 1 = Monday, etc.) |
| Today.getMonth() |
Retrieves the month
(0 = Jan, 1 = Feb., etc.) |
| Today.getFullYear() |
Retrieves the four digit
year |
| Today.getTime() |
Retrieves the time
expressed in milliseconds since December 31, 1969, 6 pm |
<script
language="JavaScript">
<!-- Hide from non-JavaScript browsers
var Today=new Date("May 20, 2004");
var ThisDay=Today.getDate();
var ThisMonth=Today.getMonth()+1;
var ThisYear=Today.getFullYear();
var DaysLeft=999;
document.write("Today is "+ThisMonth+"/"+ThisDay+"/"+ThisYear+"<br>");
document.write("Only "+DaysLeft+" days until
Christmas");
//
Stop hiding from older browsers -->
</script>
Example
2
Creating
Functions
function
function_name(parameters) {
JavaScript commands
}
function ShowDate(Date)
{
document.write("Today is "+date+<br>");
Create the
XmasDays Function
XYear -
The current Year
XDay - Christmas Day
DayCount - Number of days until Christmas
Note:
var DayCount=(XDay - CurrentDay)/<1000*60*60*24);
Create
the Function:
<script
language="JavaScript">
<!-- Hide from non-JavaScript browsers
function XmasDays(CurrentDay) {
var XYear=CurrentDay.getFullYear();
var XDay=new Date("December, 25, 2004");
XDay.setFullYear(XYear);
var DayCount=(XDay-CurrentDay)/(1000*60*60*24);
DayCount=Math.round(DayCount);
return DayCount;
}
Call the
Function
<script
language="JavaScript">
<!-- Hide from non-JavaScript browsers
var Today=new Date("May 20, 2004");
var ThisDay=Today.getDate();
var ThisMonth=Today.getMonth()+1;
var ThisYear=Today.getFullYear();
var DaysLeft=XmasDays(Today);
document.write("Today is "+ThisMonth+"/"+ThisDay+"/"+ThisYear+"<br>");
document.write("Only "+DaysLeft+" days until Christmas");
Example
3
Create a
Conditional Statement
if (DaysLeft>) {
document.write("Only "+DaysLeft+" days until Christmas");
} else {
document.write("Happy Holidays from the North Pole");
}
Example
4
Reference
New Perspectives by Thompson
Next Month: June 17, 2004
Building a web site using FrontPage
|