Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect depending on what time of day it is 1

Status
Not open for further replies.

keepingbusy

Programmer
Joined
Apr 9, 2000
Messages
1,470
Location
GB
Sorry all, but I trawled the threads of this forum and I know it's somwhere, but cant find it.

Simple question:

I need to redirect a webpage using javascript (or other method) depending on what time of day it is.

If the time is =>0600 and time is <=1800 then go to page1

or

if the time is >1800 and time is <0600 then go to page2


My experience is better in other places but I would be grateful if someone could point in the right direction.

Many thanks

Lee

Visual FoxPro Versions: 6 & 9
Operating System: Windows XP
 
Your best bet is to handle this server side rather than client side. With client-side code, you're counting on the individual's computer clock to be correct, as well as counting on them having Javascript turned on. With server-side scripting, you'll get more consistent results.

Lee
 
trollacious

Good point but it's not really an issue. The project is in relation to a webcam located in Spain which will be located in a shop. Obviously when the shop is closed I need to redirect the visitor to a "This webcam is currently offline..." sort of page.

Hope that makes sense

Thanks for your post

Lee

Visual FoxPro Versions: 6 & 9
Operating System: Windows XP
 
Then you'd need to adjust for time zones, hoping that the client computer was set up for the correct time zone (default is usually Pacific time for Windows machines, unless you change it).

If you insist on doing this client side, you'd get the time from the client computer, get the GMT offset for the client computer, then subtract the the GMT offset for the shop's time zone from the client computer GMT offset to get total time difference and add to the time on the client computer to see what the actual shop time is.

I'd only have the page redirect if the shop was closed, using

Code:
location.href='shopclosed.htm';

and let the page load otherwise with the webcam image.

Lee
 
Ok

Below is what I found but redirecting by date. As mentioned I need my page to redirect to one of two other pages depending on the time:
Code:
<SCRIPT LANGUAGE="Javascript"><!--

function initArray() {
	this.length = initArray.arguments.length;
	for (var i = 0; i < this.length; i++)
	this[i+1] = initArray.arguments[i];
}

var weekDayArray = new;
initArray("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var today = new Date();
var day = weekDayArray[today.getDay()+1];

if (day == "Monday") window.location = "Monday.htm"
if (day == "Tuesday") window.location = "Tuesday.htm"
if (day == "Wednesday") window.location = "Wednesday.htm"
if (day == "Thursday") window.location = "Thursday.htm"
if (day == "Friday") window.location = "Friday.htm"
if (day == "Saturday") window.location = "Saturday.htm"
if (day == "Sunday") window.location = "Sunday.htm"
//-->
</SCRIPT>
So as metioned at the start of the thread I am looking to this:

If the time is =>0600 and time is <=1800 then go to page1

or

if the time is >1800 and time is <0600 then go to page2


I don't have much knowledge of Javascript

Many thanks

Lee


Visual FoxPro Versions: 6 & 9
Operating System: Windows XP
 
Surely people viewing the webcam will be able to tell the store is closed because there will be no-one in it?

That aside, what about weekends? Do the shop owners really have the same opening hours on weekends as on weekdays?

Anyway - to find the time, this will work:

Code:
var now = new Date();
alert('Current hour: ' + now.getHours());
alert('Current min : ' + now.getMinutes());

Putting it together with a check for the time, you get:

Code:
<script type="text/javascript">
<!--
	var now = new Date();
	var hour = now.getHours();


	if (hour >= 6 && hour <= 17) {
		location = 'page1.html';
	} else {
		location = 'page2.html';
	}
//-->
</script>

Of course, this will show page1 at 1759, and page2 at 1800, but I'm sure 1 minute for the sake of simplicity isn't an issue.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan

I'm sure you may have seen the previous post I created but as it has now been edited, I wanted to say a "big thanks" for your help.

Lee

Visual FoxPro Versions: 6 & 9
Operating System: Windows XP
 
... And forgot to mention sorry, that I tested your code and of course, it worked perfectly.

Thanks again

Lee

Visual FoxPro Versions: 6 & 9
Operating System: Windows XP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top