Click to See Complete Forum and Search --> : need help


doingtheunstuck
05-14-2003, 12:16 PM
i'm a complete n00b when it comes to javascript... mainly because the syntax of it has always alienated me a little...

but now i've ran into something where i need to use it... what i need to do is make something where when a user clicks on a hyperlink, the script determines what day it is.. and thus sends them to a page specific for the day of the week.

i've tried to use my basic knowledge to construct something, and i've tried taking pre-made scripts meant for something that seemed similar and altering those... all with no avail.

so can anyone help? because i was hoping to get this site i'm making up before too long...

thanks.

Jona
05-14-2003, 12:31 PM
I would, if at all possible, do this server-side, because 10% of your visitors (statistically) will have JavaScript disabled on their computer. Anyhow, this will probably work (put it in your page where you want the link to appear):

<script type="text/javascript">
var d = new Date();
d = d.toString();
d = d.substring(0, 3);
switch (d){
case "Mon": d="monday.html"; break;
case "Tue": d="tuesday.html"; break;
case "Wed": d="wednesday.html"; break;
case "Thur": d="thursday.html"; break;
case "Fri": d="friday.html"; break;
case "Sat": d="saturday.html"; break;
case "Sun": d="sunday.html"; break;
}
document.writeln("<a href=\""+d+"\">"+d+"</a>");
</script>

AdamBrill
05-14-2003, 12:46 PM
If you do it like this:
(this goes in the head...)
<script language=javascript type="text/javascript">
function clickLink(){
var d = new Date();
d = d.toString();
d = d.substring(0, 3);
switch (d){
case "Mon": d="monday.html"; break;
case "Tue": d="tuesday.html"; break;
case "Wed": d="wednesday.html"; break;
case "Thur": d="thursday.html"; break;
case "Fri": d="friday.html"; break;
case "Sat": d="saturday.html"; break;
case "Sun": d="sunday.html"; break;
}
document.location=d;
}
</script>
(this is the link)
<a href="default.htm" onclick="clickLink(); return false;">Click here!</a>

Then it will go to default.htm if they don't have javascript and to the right place if they do. That way at least it won't completely fail for people without javascript...

Jona
05-14-2003, 12:52 PM
document.location? I would use location.href, as my preference.. But, it doesn't really matter because it works in both browsres.