473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

Script that changes the website background acording to the time. - HOT!

matheussousuke
249 100+
I found it on web and customized it on my own way; the credits of the author are in there. Actually it was a script to change the background color, and had other stuff on the screen that, from my point of view, was unnecessary.

Customized it to be as simple as possible, before, it was configured to load the image on the body, which was breaking my CSS, specially in one of my websites that uses CMS JOOMLA, then I had to find a way to make it load on a div. So, there you go:

Make a folder and call it "daynight", it has to be at the same directory as your page (index.php, index.html, etc.) file.

Open page's file, where you gonna change the background and post the following between the <head> tags </head>:


Expand|Select|Wrap|Line Numbers
  1. <link href="../daynight/css/CSSTimeSwitch.css" rel="stylesheet" type="text/css">
  2.  
  3. <script type="text/javascript" src="../daynight/js/timeswitch.js"></script>
  4.  
Inside the body tag of your HTML, post this:

Expand|Select|Wrap|Line Numbers
  1. onload="CSSTimeSwitch();"
That's the part responsible for loading the js once the page loads.

So it gonna look like that:
Expand|Select|Wrap|Line Numbers
  1. <body onload="CSSTimeSwitch();">

Now make a folder inside "daynight" and name it "css", make another one and call it "js".

Inside css, make a file and name it "CSSTimeSwitch.css" (If you're using windows, don't forget to configure the OS to show extensions of system files).

open the css file and paste the follow:
ps: You can change the images path to match yours.

Expand|Select|Wrap|Line Numbers
  1. /*--- after 0:00  -----*/
  2. #bgi.night {background:url(../img/night.jpg) top no-repeat;}
  3.  
  4. /*--- after 6:00  -----*/
  5. #bgi.aftersixam {background:url(../img/traveling.jpg) top no-repeat;}
  6.  
  7. /*--- after 8:00  -----*/
  8. #bgi.aftereightam {background:url(../img/morning.jpg) top no-repeat;}
  9.  
  10. /*--- after 12:00  -----*/
  11. #bgi.noon {background:url(../img/restaurants-goers.jpg) top no-repeat;}
  12.  
  13. /*--- after 13:00  -----*/
  14. #bgi.afternoon {background:url(../img/Seaside_Resort_02.jpg) top no-repeat;}
  15.  
  16. /*--- after 17:00  -----*/
  17. #bgi.aftefivepm {background:url(../img/Holiday.jpg) top no-repeat;}
  18.  
  19. /*--- after 20:00  -----*/
  20. #bgi.evening {background:url(../img/treesnight.jpg) top no-repeat;}


Open the js folder, make a file, name it "timeswitch.js" and paste the follow:
Expand|Select|Wrap|Line Numbers
  1. // © copyright Sibran Lens
  2. // Leave this comment if you want to use this script for your website
  3. // http://blog.e-ss.be
  4.  
  5.  
  6.  
  7. function CSSTimeSwitch()
  8. {
  9.  datetoday = new Date();
  10.  timenow=datetoday.getTime();
  11.  datetoday.setTime(timenow);
  12.  thehour = datetoday.getHours();
  13.  
  14.  if (thehour >= 20)
  15.   display = "evening";
  16.  
  17.  
  18. else if (thehour >= 17)
  19.   display = "afterfivepm";
  20.  
  21.    else if (thehour >= 13)
  22.   display = "afternoon";
  23.  
  24.  else if (thehour >= 12)
  25.   display = "noon";
  26.  
  27.  else if (thehour >= 8)
  28.   display = "aftereightam";
  29.  
  30.  else if (thehour >= 6)
  31.   display = "aftersixam";
  32.  
  33.  else if (thehour >= 0)
  34.   display = "night";
  35.  
  36.  
  37.  
  38.  var thebody =    document.getElementById("bgi");
  39.  
  40.  thebody.className= display;
  41.  
  42.  
  43. }
The part
Expand|Select|Wrap|Line Numbers
  1. var thebody =    document.getElementById("bgi");
is the part responsible for loading the image inside your page div or table (I didn't test the table part).
That means you've got have a div in the html that has the id "bgi", e.g.: <div id="bgi">, or table <table id="bgi">(Test the table and tell me later =]).
If you want to load the image from the body, just delete the hash tags inside the css file and rename bgi to your body id.
e.g.:
Change
Expand|Select|Wrap|Line Numbers
  1. #bgi.noon {background:url(../img/restaurants-goers.jpg) top no-repeat;}
To
Expand|Select|Wrap|Line Numbers
  1. yourbodyid.noon {background:url(../img/restaurants-goers.jpg) top no-repeat;}


It had a part that was supposed to reset the bg, just in case the user has internet explorer 6, but after all, it was just going to insert a grey bg, and Google stopped offering support to IE6, once it is a very old browser.
Today, every browser supports java script, so if you're having trouble on running the script on FF, or, Safari, Opera, Chrome, maybe you have disabled your browser java script functionality.
Well, I'll post the whole original script here as an attachment, so you can download it and use it at your own will. Don't forget to remove the Google Adwords part, it is a traffic tracker, it's inside CSSTimeSwitch.html.

Well, that is it.


Have a day as cool as you.
SB^}
Attached Files
File Type: zip css-time-switcher.zip (12.6 KB, 331 views)
Jul 3 '11 #1
0 5224

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

Similar topics

0
by: Dmitry Pljonkin | last post by:
Hi! I have some troubles trying to start php script using exec() and leave it running in the background. Manual says: "If you start a program using this function and want to leave it running...
7
by: Adam | last post by:
Hi, I am working on a project that requires me to parse 52,005,167 rows in a MySQL table. I am using a series of PHP scripts for each test I would like to run on the data. For example I have...
7
by: Arun | last post by:
Hi, This is a scripting question, but since I am writing the script in python I am posting this question here: I have a python script that runs a simulator (that was written in c++, so I use...
1
by: vasanth kumar | last post by:
Hi All, Is it possible to run a script after specified amount of time after login. I made an online test tool. I want to end the test after 20 minutes. At present one who is writing the exam...
5
by: DavidB | last post by:
Greetings I don't know if this is possible, but this group should be able to tell me. I have a webpage with a changing message board (I understand the problems with having changing text, but...
2
by: Jonah Bishop | last post by:
What is the appropriate way to start a PHP script in the background? I have a script that manipulates image data, and it takes a long time to run. I'd like to send this process to the background at...
1
by: William Sullivan | last post by:
I've got a website that may, on occasion, display a large list of items in a bulletedlist control. On the client side, it takes about 4 seconds to get a response that weighs in at over 1mb. It...
13
by: wattersmt | last post by:
Hello, I am trying to write a python cgi that calls a script over ssh, the problem is the script takes a very long time to execute so Apache makes the CGI time out and I never see any output. ...
3
by: mayafshar | last post by:
I need to change the background of my website, the design and color and I don't know how to change it in CSS? somebody design it for me and I'm not happy with it at all. also I have a image suffler...
3
by: beary | last post by:
Hi, My php website is showing the wrong time. I don't think it's a timezone thing. The website is running purely on my local machine under localhost. It's around 10 hours slow, which means when I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.