date - Javascript to change image depending on time -



date - Javascript to change image depending on time -

basically, i've got script changes page background depending on time.

<script language="javascript"> day=new date() //..get date x=day.gethours() //..get hr if(x>=0 && x<4) { document.write('<style type="text/css">#header{background: white url(images/assets/1st.jpg); color: black}"></style>') } else if(x>=4 && x<12) { document.write('<style type="text/css">#header{background: white url(images/assets/2nd.jpg); color: black}"></style>') } else if(x>=12 && x<18) { document.write('<style type="text/css">#header{background: white url(images/assets/3rd.jpg); color: black}"></style>') } else if (x>=18 && x<24) { document.write('<style type="text/css">#header{background: white url(images/assets/4th.jpg); color: black}"></style>') }

so can see, first background changes between @ 4am , on.

what alter background @ different times every day, reading sort of text file or something. example, on 10th june first background changes @ 4:15am , others different times, on 11th june first background changes @ 4:22am or , on.

could perchance find me or write me this? can't find anywhere!

thanks ever much

let's start rewriting script in more modern form (i.e. using script tag not deprecated, declaring variables...), , putting settings in array create more configurable:

<script type="text/javascript"> var = new date(); var date = (now.getmonth() + 1) * 100 + now.getdate(); var time = now.gethours() * 100 + now.getminutes(); var settings = [ { date: 1224, time: 2400, image: 'xmas.jpg'}, // christmas eve { date: 704, time: 2400, image: 'bang.jpg'}, // 4th of july // other day: { date: -1, time: 400, image: '1st.jpg'}, { date: -1, time: 1200, image: '2nd.jpg'}, { date: -1, time: 1800, image: '3rd.jpg'}, { date: -1, time: 2400, image: '4th.jpg'} ]; var setting; (var = 0; < settings.length; i++) { var s = settings[i]; if ((s.date == -1 || s.date == date) && time < s.time) { setting = settings[i]; break; } } document.write('<style type="text/css">#header{background: white url(images/assets/'+setting.image+'); color: black}</style>'); </script>

note: removed spurios "> in style sheet code.

edit:

i added code handling dates, , simplified time format. @ end of settings have items date: -1 apply date not before in settings.

javascript date time background

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -