JavaScript Problem?
Closed this issue · 4 comments
I'm working on applying JavaScript to a syllabus with multiple dates coded in @id attributes on
elements, like this:
`
<tr id="d2015-09-04">
...
</tr>
I'd like to make a link at top of this page that dynamically adjusts, day-by-day to the current date and aligns that with the nearest earlier date on the syllabus. I first wrote the JavaScript simply to match today's date with the part of the date in my @id attributes after the "d," which was pretty straightforward. But then I realized that on Tuesdays or Thursdays, today's date would no longer match the dates in the @id attributes. I need a way to adjust so JavaScript reviews the available array of dates and pulls the nearest earlier date to the current one. So, if today were Thurs. 9/24, I would want my JavaScript to look for the nearest earlier day coded in an @id on the syllabus, or Wed. 9/23. I'm not sure if I've done this right, and I'm afraid I won't know until it's actually Thurs. 9/24, but I think what I've coded probably will NOT do what I think it will. Here's my JavaScript file:
https://github.com/ebeshero/newtFire-webDev/blob/master/dh-course/jumpingDateLinks.js
To see it in action (and have a look on Thurs. 9/24 with me to see if this breaks, as I expect it will, here's the HTML page: http://newtfire.org/dh/CDASyll.html . Click on the link that reads "Jump Down to the Schedule" and see where it takes you. Today it works. I bet tomorrow it won't. And on Friday it will, but on Saturday and Sunday it won't--unless I figure out how to do this properly by then!
It's doing what it's supposed to now, and jumping right down to Wed. 9/23 because the days match. But will it break on Thurs. 9/24?
A temporary, if not ideal solution: I've generated via XSLT an HTML page with <a/>
elements inside the table rows with @id attributes holding each date in between the syllabus class days. I applied a series of conditional statements to work out the dates I'd need between a Friday and a Monday, and between Mondays and Wednesdays, and I worked around Fall Break and Thanksgiving Break and even Finals Week, though dates stop at the weekend following Finals. The page will "Jump" where we need it to at least during this semester!
Thanks, @djbpitt , for finding a good way to make JavaScript search through my array of dates (in reverse!) to find the closest preceding match to today's date. Huzzah! The JavaScript now works.
Uh oh! On Friday, 10/2, the first class day after implementation of the new JavaScript, it failed to generate a link to that day's class period. But on Saturday 10/3, the JavaScript functions. We have a problem here, that our new JavaScript only functions on days when we do NOT have class.
My speculation is that this line of code is the problem:
if(dates.indexOf(today)!=-1){ target = today;
JavaScript was revised and is functional; I just neglected to close this issue.