Different URLs based on day of the week?
January 18, 2008 9:24 AM   Subscribe

Javascript question: How can I make a link point to different URLs based on the day of the week?

Got a single link that needs to go to a different URL for each day of the week. Currently, I'm going in and updating this "What's happening today" page manually, but that's obviously not the best way to do it - I want there to be seven different pages, so I can do the week's content in advance. My Javascript skills are poor. Can some kind soul please help?
posted by jbickers to Computers & Internet (7 answers total)
Use a Date object (http://www.w3schools.com/js/js_obj_date.asp). Something like,

var d = new Date();
var num = d.getDay();

From there, you have the day; just concatenate it with some base URL and you're all set.
posted by spaceman_spiff at 9:30 AM on January 18, 2008

Well, I started to write a script that would do this, but then thought someone already had. I was right. Try this.
posted by dirtdirt at 9:32 AM on January 18, 2008

So, to be clear: you have all your links point to a page called "whats_happening.html" (or whatever), and whats_happening.html consists just of this script. Then you have your seven pages called "monday.html", "tuesday.html" and so forth, which are what your user ends up being redirected to when they click on a link to "whats_happening.html"
posted by dirtdirt at 9:51 AM on January 18, 2008

In an onclick for a link, or onload for the page:

window.location = [ 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' ][new Date().getDay()] + '.html';

NB it's not ideal to do this on the client-side.
posted by moift at 9:56 AM on January 18, 2008

Well, I should have specified - this is within a CMS, so I don't have control over the names of the seven URLs. I think I'm looking more for an "if Monday, go here, if Tuesday, go here" solution. Is that possible?
posted by jbickers at 11:03 AM on January 18, 2008

In the one I linked change the name of those seven variables, that are currently days, to the names of the pages. Javascript's day is a number, this script uses that number to call an element in an array. That element can contain anything you like, currently it is days of the week.
posted by dirtdirt at 11:06 AM on January 18, 2008

How is that different? Change 'monday' to 'here' and 'tuesday' to 'there', and remove the + '.html' if the urls don't use that extension.
posted by moift at 11:09 AM on January 18, 2008

« Older Organizing Ideas for a company brainstorm: Wiki...   |   Recommend me an effective Austin company that will... Newer »
This thread is closed to new comments.