473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Javascript to change image depending on time..

Hi there,

Basically, I've got a script which changes the page background depending on the time.


Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2.     day=new Date()     //..get the date
  3.     x=day.getHours()    //..get the hour
  4.  
  5.     if(x>=0 && x<4) {
  6.  
  7.        document.write('<style type="text/css">#header{background: white url(images/assets/1st.jpg); color: black}"></style>')
  8.  
  9.     } else
  10.  
  11.     if(x>=4 && x<12) {
  12.  
  13.        document.write('<style type="text/css">#header{background: white url(images/assets/2nd.jpg); color: black}"></style>')
  14.  
  15.     } else
  16.  
  17.     if(x>=12 && x<18) {
  18.  
  19.        document.write('<style type="text/css">#header{background: white url(images/assets/3rd.jpg); color: black}"></style>')
  20.  
  21.     } else
  22.  
  23.     if (x>=18 && x<24) {
  24.  
  25.        document.write('<style type="text/css">#header{background: white url(images/assets/4th.jpg); color: black}"></style>')
  26.  
  27.     }
  28. </script>
So as you can see, the first background changes between at 4am and so on.

What I would look to do is to change the background at different times every day, reading from some sort of text file or something. For example, on the 10th June the first background changes at 4:15am and the others with different times, on the 11th June the first background changes at 4:22am or something and so on.

Could someone possibly find me or write me something to do this? I can't find anything anywhere!


Thanks ever so much
Jul 6 '11 #1
4 2864
Rabbit
12,516 Expert Mod 8TB
Just so you know, your current script can be simplified to
Expand|Select|Wrap|Line Numbers
  1. day=new Date() //..get the date
  2. x=day.getHours() //..get the hour
  3.  
  4. document.write('<style type="text/css">#header{background: white url(images/assets/' + Math.floor(x/4) + '.jpg); color: black}"></style>')
As for your question, you will have to hard code it in to your javascript unless you plan on using server side scripting.
Jul 6 '11 #2
Thanks for the simplification.
Yeah I think I'd go for with hard coding it into the JS as it'd be easier.
Jul 6 '11 #3
Rabbit
12,516 Expert Mod 8TB
Just a quick note but for the simplification, you'll have to rename the image files 1, 2, 3, and 4. Not 1st, 2nd, 3rd, 4th.
Jul 6 '11 #4
Managed to sort it :) For reference if anyone else wants such functionality: http://stackoverflow.com/questions/6...ending-on-time

Thanks for the help though Rabbit
Jul 7 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
2
by: revna | last post by:
I want to change my System clock with an c# programm, but dateTime are readonly, or? thanx for help
1
by: muhamad | last post by:
hi i use the com AxShockwaveFlashObjects and i use a thread ,in the thread i use this object ,if i can't use this more than 5 minutes ,i get a error i think the problem is in the life time...
0
by: zPaul | last post by:
I would like to change image that says "loading" while loading a file. If that is not possible, change text in label. I was able to change the image in ASP but, can't do it in dotnet. Does...
6
by: ruca | last post by:
Hi gurus, I have a imagebutton in my WebForm, and I want that when I click (mouse down) on her the imagebutton change image and when I "unclick" (mouse up) change to the original image. Basically...
1
by: Tom S. | last post by:
Hello all, I have a page that contains two listboxes and an "Add" button. When clicked, the button fires javascript that moves the selected item(s) in the first listbox to the second listbox and...
8
by: fniles | last post by:
Can I change the connection time out in VB.Net 2003 and MS Access (using OLEDbConnection) ? When I use Connect Timeout = 60 in the connectionstring, I get an error "Multiple-step OLE DB operation...
7
by: kadarkarai | last post by:
I am developing photo gallery in Macromedia Flash 8 and XML to retrive the Image. Image Size is all in different size. my question is How to Change image size at runtime in flash? so that image...
1
by: newsteve1 | last post by:
hello, i have a parent page with a bunch of photos. clicking on the first photo will lead to a new child window with a close-up of that photo AND from there you can cycle through all of the other...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.