Jump to content

different page for times of day


nobis77
 Share

Recommended Posts

Hello all, first post here. I want to be able to have someone arrive at a landing page and be automatically taken to another based on the time of day.I know this is probably something that is sorta covered elesewhere,and I appologize for that, but I've been searching on and plinking at this for days and getting nowhere. I figured it was just time to ask for exactly what I need. Please keep in mind that I'm a complete newbie. Here's a sample of my last (unsuccessful) attempt:

<script language="javascript" type="text/javascript">function sethour(){var now = new Date();var hour = now.getHours();var load = 'page.htm';if (hour = 0) load = 'http://www.site.com/0.html';if (hour = 1)load = 'http://www.sitte.com/1.html'; if (hour = 2) load = 'http://www.site.com/2.html';														 ...so on and so forth...window.location.href=load;}</script>

I've tried other variations, I'm down to just taking pot-shots. I've seen warnings that line spacing and semicolons are absolutely critical and then seen examples that are completely incompatible. Been told to abandon this cause javascript can't do it after seeing the ."night and day" script for absolute beginers. Fraid I need some exact code and s'pecific instructions to load atleast one page, and get on the board. I hope I can take it from there. Please help, I even had a weird dream of broken bits of javascript the other night!

Link to comment
Share on other sites

You can do pretty much whatever you want.If you make the decision that the file name will correspond to the hour - the page to show at 5AM is called 5.html and the page to show at 5PM is 17.html - then you can simplify your code quite a bit:

var date = new Date();var hour = date.getHours();var load = "http://www.site.com/" + hour + ".html";

Otherwise, you can use a switch:

var date = new Date();var hour = date.getHours();var load = "http://www.site.com/";switch(hour){	case 0:		load += "0.html";		break;	case 1:		load += "x.html";		break;	case 2:		load += "somepage.html";		break;	case 3:		load += "3.html";		break;	// and so on}

Link to comment
Share on other sites

Thanks everyone, I think I'll try jesh's first option, first.Aspnetguy: thanks, I'll be looking into serverside via php as soon as I get the chance. Oddly, though, I've been told the opposite about javascript - new Date -and user's time; that it pulls the server time, could you confirm one way or the other, it's not critical here, but it would be nice to knowJesh: very clean and simple looking solution, thanks again.

Link to comment
Share on other sites

Aspnetguy: thanks, I'll be looking into serverside via php as soon as I get the chance. Oddly, though, I've been told the opposite about javascript - new Date -and user's time; that it pulls the server time, could you confirm one way or the other, it's not critical here, but it would be nice to know
new Date() would instantiate a Date object on the computer executing that code and would, therefore, use the time that is running on that computer. Because, for the most part, javascript is client-side, executing new Date() in javascript would use the time on the client computer, not the server.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...