Help me improve mobile access to the state legislature website.
January 16, 2010 9:58 AM

HTML question: Is it possible to change an href location in a link based on today's date with javascript?

I spend a lot of time monitoring the Kansas Legislature and complaining about the inaccessibility of their website on mobile devices. I'm amazed that the norm is to kill off a small forest to print each days important reference documents that could more easily be used electronically.

I've built a simple little iphone webpage to help to help myself and a couple friends gain quick access to the happenings and important daily documents of the Kansas legislative session. 90% of it was dead simple because the Kansas Legislature website uses redirect links like "http://www.kslegislature.org/legsrv-calendars/curHouseCalendar.do" which will always take you to the most recent house calendar. However there are 3 more daily important documents that do not use this convention and instead link directly to a dated PDF whose name is based on the date: "http://www.kslegislature.org/agstat/2010/hs0119.pdf" is the House Status document for Jan 19. (Specifically I am referring to the House status & agenda documents and the Senate combined status/agenda doc all found here.

Is there a short little javascript that would construct a link URL based on today's date that I could use to provide easy access to these docs? Extra credit if you can help me avoid the inevitable problem of weekends where there is no new documents published.
posted by jlowen to Computers & Internet (12 answers total)
Short answer, yes, it would be built on these functions. I don't have time to write one out myself now, though. Some pseudo code:

get date
if date.day is 6, date = date - 1
if date.day is 7, date = date - 2
end if
link = string + date.year + "/hs" + date.month + date.date + ".pdf"

document.write(link)

or something like that.
posted by The Michael The at 10:24 AM on January 16, 2010


If you use jQuery, it'd be something like

var newtarget = (calculate the date)
$("a").attr("href", newtarget)
posted by d. z. wang at 12:26 PM on January 16, 2010


I haven't checked it, but this should give you links to the last three days of records. It will ignore 29th Feb because I'm drinking beer... :)
function dateString(past)
{
    var days = new Array();
    days[0]=31;    // J
    days[1]=28;    // F
    days[2]=31;    // M
    days[3]=30;    // A
    days[4]=31;    // M
    days[5]=30;    // J
    days[6]=31;    // J
    days[7]=31;    // A
    days[8]=30;    // S
    days[9]=31;    // O
    days[10]=30;    // N
    days[11]=31;    // D
    
    var d = new Date();
    var dow = d.getDay();
    var dom = d.getDate();
    var month = d.getMonth();
    var year = d.getYear();

    if (dow == 6)
        dom -= 1;
    if (dow == 0)
        dom -= 2;
    if (dom<1>
        month -= 1;
        if (month<1>
            year -= 1;
            month = month+12;
            }
        dom = days[month-1];
        }
    if (dom<1>
    if (month<1>
    document.write('<a href="http://www.kslegislature.org/agstat/'+year+'/hs'+dom+month+'.pdf">'+dom+'/'+month+'/'+year+'</a> ');
}
    
dateString(0);
dateString(1);
dateString(2);

posted by twine42 at 2:29 PM on January 16, 2010


Oh, html, you sod. The <s went wrong...
function dateString(past)
{
    var days = new Array();
    days[0]=31;    // J
    days[1]=28;    // F
    days[2]=31;    // M
    days[3]=30;    // A
    days[4]=31;    // M
    days[5]=30;    // J
    days[6]=31;    // J
    days[7]=31;    // A
    days[8]=30;    // S
    days[9]=31;    // O
    days[10]=30;    // N
    days[11]=31;    // D
    
    var d = new Date();
    var dow = d.getDay();
    var dom = d.getDate();
    var month = d.getMonth();
    var year = d.getYear();

    if (dow == 6)
        dom -= 1;
    if (dow == 0)
        dom -= 2;
    if (dom<1) {
        month -= 1;
        if (month<1) {
            year -= 1;
            month = month+12;
            }
        dom = days[month-1];
        }
    if (dom<10)    dom = '0'+dom;
    if (month<10)    month = '0'+month;
    document.write('<a href="http://www.kslegislature.org/agstat/'+year+'/hs'+dom+month+'.pdf">'+dom+'/'+month+'/'+year+'</a> ');
}
    
dateString(0);
dateString(1);
dateString(2);

posted by twine42 at 2:32 PM on January 16, 2010


Here's a JavaScript function and a button to call it that will open today's page if it's a weekday.
<script type="text/javascript">

function openToday() {

    // Get year, month, and day right now
    var now = new Date(),
        year = now.getFullYear().toString(),
        // Months are indexed from zero
        month = now.getMonth() + 1,
        day = now.getDate(),
        weekday = now.getDay();

    if ((weekday == 0) || (weekday == 6)) {
        // It's the weekend
        return false;
    }

    // Ensure that month and day have two digits
    (month < 10) && (month = '0' + month.toString());
    (day < 10) && (day = '0' + day.toString());

    // Construct the URL
    var url = (
        'http://www.kslegislature.org/agstat/' +
        year + '/hs' + month + day + '.pdf'
    );

    // Go there
    window.location.href = url;

}

</script>
<input type="button" value="Open today's page" onclick="openToday()">

posted by letourneau at 2:37 PM on January 16, 2010


Not much more to add since letourneau pretty-much nailed it on the head, but a couple of additional pieces of info that you might find helpful:
  1. The .do extension indicates they're running a Java webapp, most likely using Struts. You might have some luck talking to their devs about building a (very) simple, open API for others to access this info more easily.
  2. Their document directories are wide-open. Just FYI.

posted by Civil_Disobedient at 5:54 PM on January 16, 2010


You're talking about something like the Congressional Record Latest Daily Digest link on the THOMAS homepage, right? Like how it always links to the latest one, skipping weekends (except, of course, when there's a weekend session) and other, seemingly random days when Congress isn't in session? I can tell you how that's done, if you like.
posted by MrMoonPie at 8:04 PM on January 16, 2010


Thanks everyone for your help. This is brilliant.

I was able to get letourneau's script to work perfectly after I realized that the reason It was failing to work was because I was trying to click on it on a weekend day. I changed my computer clock to a weekday and all was well.

What would be the most intuitive way to display a popup message alerting the user that the link does not function on a weekend? Maybe something like, "The document can not be found because the House is not in session today. You may find more information on the full website."

Also, what is the best way to call the function via href and not via a form button? I see different possibilities when I google around.

Thanks again!
posted by jlowen at 9:21 PM on January 16, 2010


OK, revised version that uses a link rather than a button. Note that here the link has to come before the script (because the script attaches the proper URL or action to the link, and so the link has to exist before the script runs).
<a id="todaysLink">Open today's page</a>
<script type="text/javascript">

// Get year, month, and day right now
var now = new Date(),
    year = now.getFullYear().toString(),
    // Months are indexed from zero
    month = now.getMonth() + 1,
    day = now.getDate(),
    weekday = now.getDay();

if ((weekday == 0) || (weekday == 6)) {
    // It's the weekend
    document.getElementById('todaysLink').href = (
        'javascript:alert("Sorry, nothing on weekends.")'
    );
    return;
}

// Ensure that month and day have two digits
(month < 10) && (month = '0' + month.toString());
(day < 10) && (day = '0' + day.toString());

// Construct the URL
var url = (
    'http://www.kslegislature.org/agstat/' +
    year + '/hs' + month + day + '.pdf'
);

// Attach it to the link
document.getElementById('todaysLink').href = url;

</script>

posted by letourneau at 9:34 PM on January 16, 2010


Alternately, instead of:
document.getElementById('todaysLink').href = (
    'javascript:alert("Sorry, nothing on weekends.")'
);
...you could do:
document.getElementById('todaysLink').innerHTML = (
    'Sorry, nothing on weekends.'
);
...which would make the link into a non-clickable message.
posted by letourneau at 9:40 PM on January 16, 2010


You can also do it as <a href="#" onclick="openToday()">. (Make sure to have the function return false if it doesn't change the location so as to stop event processing.)
posted by Rhomboid at 2:23 AM on January 17, 2010


Perfect! I've used letourneau's suggestions to accomplish exactly want I wanted.

Thanks all for your help!
posted by jlowen at 10:11 AM on January 17, 2010


« Older How to fake a forest?   |   Another word for "ringer" Newer »
This thread is closed to new comments.