Click to See Complete Forum and Search --> : Using Date for new Calendar
Shiesl
12-14-2002, 12:32 PM
I have different Monthly Calendar pages with different selected events. I simply wish to go to a different page, e.g. 12-2002.htm, 1-2003.htm, based on the date. My present HTML is:
<tr>
<td><a href="javascript:;">Calendar</a></td>
</tr>
But I'm not sure what to replace the "javascript:;" with.
I have a function I started, but also am not sure it's right:
<head>
<script language = "JavaScript">
<!-- Begin
function GetMonthYear {
var Calendar = new Date();
var year = Calendar.getYear(); // Returns year
var month = Calendar.getMonth() + 1; // Returns month (0-11)
var MonthYear = month + year + “.htm” // or does this need to be STR(month) + STR(year) + “.htm”
return MonthYear;
}
</script>
</head>
Thanks for any and all help. rick
Charles
12-14-2002, 01:17 PM
A couple of things:
1) You shouldn't be using tables for layout. It'll cause problems for non-graphical browsers such as Braille and audio ones.
2) The LANGUAGE attribute of the SCRIPT element was depricated and if you use it then Netscape will behave in some non-standard ways. Use instead <script type="text/javascript">
3) Something like 12% of users do not use JavaScript so you need a sceme for keeping things working for those people. If you are using href="javascript:..." then you are most often in trouble.
4) JavaScript converts data types automatically. You can also force a conversion to a number or a string, but JavaScript doesn't have a cast like some other languages. Instead, all objects have a valueOf and a toString method that you can call. id est month.toString().
Your best bet is to move the script to the server, but if you promise to put a page of links to the different months at, say, 'calendar-index.html' for the people like me who eschew JavaScript then you can change your link to:
<a href="calendar-index.html">Calendar</a>
<script type="text/javascript">
<!--
document.links[document.links.length-1].onclick = function () {
now = new Date();
window.location = (now.getMonth() + 1) + '-' + now.getFullYear() + '.htm';
};
// -->
</script>
Shiesl
12-14-2002, 02:45 PM
Charles -
Thank you greatly for your aid.
I agree that I should not be using tables for layout when I have time I'll attempt to switch it to CSS. This web site is for a bicycle club thus I would not expect the use of a lot of non-graphical browsers. Still, I agree it should be changed.
I cannot say that I understand your script as it is beyond my skill, but I will use it and see what happens. I thank you again for your help. rick
Charles
12-14-2002, 03:02 PM
Actually, there's an error there. That should have been:
<a href="calendar-index.html">Calendar</a>
<script type="text/javascript">
<!--
document.links[document.links.length-1].onclick = function () {
now = new Date();
window.location = (now.getMonth() + 1) + '-' + now.getFullYear() + '.htm';
return false;
};
// -->
</script>
The onclick handlers are called before the value of the HREF attribute is loaded. But, if the handler returns 'false' then that action is suppressed, which is what you want here. And all that is just another way of writing:
<a href="calendar-index.html" onclick="now = new Date(); window.location = (now.getMonth() + 1) + '-' + now.getFullYear() + '.htm'; return false">Calendar</a>
A lot of people prefer:
<script type="text/javascript">
<!--
function getIt() {
now = new Date();
window.location = (now.getMonth() + 1) + '-' + now.getFullYear() + '.htm';
return false;
};
// -->
</script>
<a href="calendar-index.html" onclick="return getIt()">Calendar</a>
but I cannot stand naming a function that's only called once. Use whichever method makes most sense to you.
Shiesl
12-14-2002, 03:02 PM
Sorry about this.
The script that Charles supplied I thought would work, but in attempting to use it I was always sent to the "Calendar-index.html" file, but no further. I'm sure I'm missing something elementary, but I have no idea what it is.
Thanks again for any help. rick
Charles
12-14-2002, 03:05 PM
It wasn't you; it was me. Please accept my apology and see above.
Shiesl
12-14-2002, 04:20 PM
Thanks once again. I'm glad I was not totally out of it.
I'll try it again - probably tomorrow - and see what happens. rick