Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Intermediate
Jun 4, 1999

Redirect Based on Time of Day Using JavaScript

Redirecting based on the time of day is fairly easy to do. One thing to remember, however, is that because client-side JavaScript runs in the user's browser, the time your code will be working with is the time set on the user's machine, not your Web server. This factor is especially important if your site gets a lot of international visitors. That said, here's one way to code a time-based redirect:
 
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function ampmRedirect(dayURL, nightURL)
{
	var currentTime;
	var currentHour;

	// get the current date and time
	currentTime = new Date();

	// returns the hour in military time
	currentHour = currentTime.getHours();
	// check if the hour is between 6pm and 6am
	if ((currentHour < 6) || (currentHour > 17)) {
		window.location = nightURL;
	} else {
		window.location = dayURL;
	}
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<FORM>
Click here to go to the next page:<BR>
<INPUT TYPE="button" VALUE="Next"
onClick="ampmRedirect('day.htm','night.htm')">
</FORM>
</BODY>
</HTML>
Boris Feldman
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date