Reputation: 11
I'm not very familiar with scripts, etc... and I have a very precise question. On one of my pages I want to redirect to a page, based on today's date. Searching the web, I've come up with something like this at the moment :
<html>
<head>
<title>test</title>
</head>
<body>
<script type="text/javascript">
window.onload = function() {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
var newurl = '/Documenten/Kalender_Datum_' + [year, month, day].join('-') + '.html';
// document.location.href = redirect_datum;
alert(newurl); // simulated redirection for testing purposes only
}
</script>
</body>
But when I want to visit the page, it doesn't do much.... Can anyone explain me what's happening ? Are there any syntax-errors, or other .... File can be found HERE THX a lot Carl
Upvotes: 1
Views: 66
Reputation: 106
The problem is in this line of code
var d = new Date(date)
date is an undefined variable. It should be
var d = new Date()
Upvotes: 2
Reputation: 4315
Remove the date
argument passed to the Date()
and wich is defined nowhere, and the script will work fine. This variable is not defined and cause an error, the script will not continue.
window.onload = function() {
var d = new Date(),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
var newurl = '/Documenten/Kalender_Datum_' + [year, month, day].join('-') + '.html';
// document.location.href = redirect_datum;
alert(newurl); // simulated redirection for testing purposes only
}
<html>
<head>
<title>test</title>
</head>
<body>
</body>
Upvotes: 1