News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Changing background script

Started by topleya, June 05, 2009, 05:54:26 PM

Previous topic - Next topic

topleya

I have been testing a changing background script that changes the bg depending on the time of the day.

It works on my main page as shown here http://www.simlish.co.uk/test.php (to see working change your computers time and refresh page)

I put the exact same code on the forums and it refuses to work and throws out a template error.

Below is the exact code I use

<script language="JavaScript">

day=new Date()     //..get the date

x=day.getHours()    //..get the hour

if(x>=6 && x<10) {

   document.write('<style type="text/css">body{background-image: url(http://www.simlish.co.uk/images/bg/dawn.jpg);} #header {background-image: url(images/site/content_top_background.png);}</style>')

} else

if(x>=10 && x<18) {

   document.write('<style type="text/css">body{background-image: url(http://www.simlish.co.uk/images/bg/day.jpg);} #header {background-image: url(images/site/content_top_background.png);}</style>')

} else

if(x>=18 && x<21) {

   document.write('<style type="text/css">body{background-image: url(http://www.simlish.co.uk/images/bg/dusk.jpg);} #header {background-image: url(images/site/content_top_background.png);}</style>')

} else

if (x>=21 && x<24) {

   document.write('<style type="text/css">body{background-image: url(http://www.simlish.co.uk/images/bg/night.jpg);} #header {background-image: url(images/site/content_top_night_background.png);}</style>')

} else

if (x>=0 && x<6) {

   document.write('<style type="text/css">body{background-image: url(http://www.simlish.co.uk/images/bg/night.jpg);} #header {background-image: url(images/site/content_top_night_background.png);}</style>')

}
</script>


Any ideas?

M-DVD

Have you put the code inside an echo statement PHP? echo 'code, code, code';

Also, you should add backslash character (\) before every quote character (') in the code

Advertisement: